Jump to content

Recommended Posts

I found another tutorial on the web for uploading files to a MySQL database, so it appears to work as far as saving the file name to my table in the database, but I have two more pages for displaying the list of files and then allowing you to download the files, but those pages are not working. The page that displays the files for downloading is displaying this error:

 

Parse error: parse error in C:\wamp\www\displayimages.php on line 28

 

Here is the code for it:

 

LIST IMAGES:

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//connect and select database
$conn = mysql_connect("localhost", "root", "")
   or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?php=$id;?>"><?php=$name;?></a> <br>

</body>
</html>

 

And here is the page for downloading the images:

 

DOWNLOAD IMAGES:

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

//connect and select database
$conn = mysql_connect("localhost", "root", "")
   or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =                                  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

//close database connection
exit;
}

?>

 

What am I doing wrong?

I found one of the } I think that I was missing, but I don't see the other one. :-( Here is the revised code. What else am I missing? The error is saying:

 

Parse error: parse error in C:\wamp\www\displayimages.php on line 20

 

Display List

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
//connect and select database
$conn = mysql_connect("localhost", "root", "")
   or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0) {
echo "Database is empty <br>";
} else {
while(list($id, $name) = mysql_fetch_array($result))
}
?>
<a href="download.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> <br>

</body>
</html>

 

Download Images:

<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

//connect and select database
$conn = mysql_connect("localhost", "root", "")
   or die(mysql_error());
mysql_select_db("smrpg",$conn) or die(mysql_error());

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =                                  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

//close database connection
exit;
}

?>

 

Am I missing anything else?

This

if(mysql_num_rows($result) == 0) {
echo "Database is empty <br>";
} else {
while(list($id, $name) = mysql_fetch_array($result))
}
?>
<a href="download.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> <br>

 

Should be

if(mysql_num_rows($result) == 0) 
{
    echo "Database is empty <br>";
}
else
{
    while(list($id, $name) = mysql_fetch_row($result))
    {
?>
<a href="download.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> <br>
<?php
    }
}
?>

 

Indenting you code makes it easier to read and maintain.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.