Have an account? Sign in
Login  Register  Facebook
Help with upload and downloading images
the code to upload images/files
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</td>
</tr>
</table>
</form>
$uploadDir = 'uploads/';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

$hostname_conndb = "localhost";
$database_conndb = "utpoia";
$username_conndb = "root";
$password_conndb = "qaasim11";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
} 

$sql = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());

echo "<br>File $fileName uploaded<br>";

}
This works fine and uploads images to the database and to the uploads folder. I cannot download the image/files The code for download is
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database

$hostname_conndb = "localhost";
$database_conndb = "utpoia";
$username_conndb = "root";
$password_conndb = "qaasim11";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_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;


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

<body>
<?php
$hostname_conndb = "localhost";
$database_conndb = "utpoia";
$username_conndb = "root";
$password_conndb = "qaasim11";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb)
 or trigger_error(mysql_error(),E_USER_ERROR);

$sql = "SELECT id, name FROM upload";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?>
<?php if($total_rows > 0) { ?> 
<?php do { ?>
<a href="download.php?id=<?php echo $rows['id']; ?>"><?php echo $rows['name']; ?></a> <br>
<?php } while($rows = mysql_fetch_assoc($result)); ?>

<?php 
} else {
	echo "<p>There are currently no images associated with this category.</p>";
}
?>

</body>
</html>
This is the code for the database:-
CREATE TABLE IF NOT EXISTS `upload2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`path` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

--
-- Dumping data for table `upload2`
--

INSERT INTO `upload2` (`id`, `name`, `type`, `size`, `path`) VALUES
(1, 'applications.png', 'image/x-png', 3456, 'uploads/applications.png'); 
and other structure
CREATE TABLE IF NOT EXISTS `upload` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`content` mediumblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

It passes the id but does not download the file.

I am testing on local server

Can you please tell me what is wrong with this code.
Started: September 21, 2011 Latest Activity: September 21, 2011 php mysql
1 Answer
try use this after making db connection:
$hostname_conndb = \"localhost\";
$database_conndb = \"utpoia\";
$username_conndb = \"root\";
$password_conndb = \"qaasim11\";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb);



if(isset($_GET[\'id\'])) {
    $id    = intval($_GET[\'id\']);
    $query = mysql_query(\"SELECT name,type,size,path
                             FROM `upload` WHERE id = \'$id\'\");
    
    list($name, $type, $size, $path) = mysql_fetch_array($query);
        if(file_exists($path)){
            header(\"Content-length: $size\");
            header(\"Content-type: $type\");
            header(\'Content-disposition: attachment; filename=\'.$name.\'\');
            header(\"Pragma: no-cache\");
            header(\"Expires: 0\");
        
            @set_time_limit(0);
    
            $fp = @fopen($path, \'rb\');
    
            if ($fp !== false)
            {
                while (!feof($fp))
                {
                    echo fread($fp, 8192);
                }
                fclose($fp);
            }
            else
            {
                @readfile($path);
            }
    
            flush();            
        }else{
            die(\'the file\'.$name.\'is non exist in \'.$path);
        }

    

}

this code will select the `path` and read the file and send it to the broser

Posted: MacOS
In: September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer