download php is always damaged - php

I have a download script written in PHP. My view file script links to the ids and then selects all the data that matches the ID.
The data is then used to download the photo. Does it matter that my photo is in a folder? It is moved to a folder and then the directory is uploaded to the MYSQL database.
The code at the moment now allows some files to download perfectly and then the majority to be damaged. Any reason why?
Mysql table info...
$cool = $_GET['id'];
$sql = "SELECT id, type, name, size FROM upload WHERE id='$cool'";
$result = mysql_query($sql, $db);
$data = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
$size = mysql_result($result, 0, "size");
$type = mysql_result($result, 0, "type");
header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
readfile($name);
exit();

This happens because PHP send some information after you echo the data, the solution to this is to stop processing right after you echoed the data, for this add exit(); right after echo $data.

Related

when i download the file from database then "failed to load" message display

I am trying to download the files from database. but it shows "failed to load" message.Uploading is working fine. i can upload pdf, jpg, dox file. but unable to open when i download.IF there are three href links in my file. first is for "upload" second is for "delete". every link download the file from image even other links are for other work but every file calls the downld.php file
<?php echo $name;?>
downld.php
<?php
if (isset($_GET['id']))
{
$project_id = $_GET['id'];
$myConnection= mysqli_connect("localhost","root","", "extra") or die ("could
not connect to mysql");
mysqli_set_charset($myConnection,'utf-8');
global $wpdb;
$sql = "SELECT name, type, size, content " .
"FROM wp_postprojectwin WHERE project_id = $project_id";
$result = mysqli_query($myConnection,$sql) or die('Error, query failed');
list($name, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
mysqli_close($connection);
ob_clean();
flush();
echo $content;
exit;
}
else{
echo "sorry";
}
?>

Image Thumbnails & Downloading Them

I have the following code for creating thumbnails of the images in the database automatically when the are uploaded.
<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos where caption='cars'");
while($row = mysql_fetch_array($result))
{
echo '<div id="imagelist">';
echo '<p><img src="'.$row['location'].'"></p>';
echo '</div>';
}
?>
Now whenever I click an image in the gallery I want it to open the image in a new tab and give me an option to download, I want the download code which will take in the photo id from the database and then give me the option to download that image only how to do it.
Try this :
$file = your file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
mysql_* functions are deprecated in new version of php, So use mysqli_* functions OR PDO

File download from server using mysql and PHP

I have created a PHP page that allows users to download a file when they click the this link:
Download File
I have also created the download page that the link directs to:
<?php
if(isset($_GET['file'])) {
$fileID = $_GET['pubid'];
$filename= ($_GET['file']);
$path = "admin/pubfiles/";
$fullPath = $path . $filename;
mysql_select_db($database_connDioceseofife, $connDioceseofife);
$sql = "SELECT file FROM publications WHERE pubID = $fileID";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if($filename == NULL) {
die('No file exists or the name is invalid!');
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($fullPath);
}
?>
Now my problem is, when the download popup window come up, it reads that the file is of 0 bytes. And when downloaded, it can't open. i get the message that it is not a supported file type or its been damaged or corrupted.
Please any help would be much appreciated.
You're not doing anything with the query result in $row.
Use $row['file'] to get the actual file itself.
Thank you all for assisting me. I have been able to solve the problem after further reading. I have updated the initial script i wrote. what is above is now the working script.
What i did was to include $fullpath = $path . $filename then changed the header("Content-Disposition: attachment; filename=\"$filename\""); and then the readfile function from readfile($path) to readfile($fullpath).
Thanks again #nlsbshtr and everybody else for your help.

Retrieving image from database - image not displayed

Here is the code I am using to try and retrieve an image from a database:
<?php
if($id)
{
//please change the server name username and password according to your mysql server setting
$mysql_server="localhost";
$mysql_username="myuser";
$mysql_password="mypass";
$mysql_database="mydb";
//connect to database using above settings
#MYSQL_CONNECT("localhost",$mysql_username,$mysql_password);
#mysql_select_db("mydb");
//select the picture using the id
$query = "select bin_data,filetype from todo where id=$id";
//execute the query
$result = #MYSQL_QUERY($query);
//get the picture data which will be binary
$data = #MYSQL_RESULT($result,0,"bin_data");
//get the picture type. It will change according to file extension it may be either gif or jpg
$type = #MYSQL_RESULT($result,0,"filetype");
//send the header of the picture we are going to send
Header( "Content-type: $type");
//send the binary data
echo $data;
};
?>
Instead of displaying the requested image, it displays this icon: (not sure what you call it)... http://i.imgur.com/bo6Jg.png
Here are all the columns in my table: http://i.imgur.com/PuWvl.png
I'm pretty positive I'm doing everything right...not sure what's going on. Help anyone? Thanks in advance!
IMO only constants should be uppercase (tho I gladly did not know they could be upper),
Anyway Try this:
<?php
$file_not_found = '../not_found_image.jpg';
//Get the id param from GET else null
$id = (isset($_GET['id']) && is_numeric($_GET['id']))?$_GET['id']:null;
if($id != null) {
$mysql_server="localhost";
$mysql_username="myuser";
$mysql_password="mypass";
$mysql_database="mydb";
//Connect to database using above settings
mysql_connect($mysql_server,$mysql_username,$mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());
//Select the picture using the id
$query = "SELECT `bin_data`, `filetype` FROM todo WHERE id=".(int)mysql_real_escape_string($id)." LIMIT 1";
//Execute the query
$result = mysql_query($query);
//Found
if(mysql_num_rows($result)==1){
//Get the picture data which will be binary
$data = mysql_result($result,0,"bin_data");
//Get the picture type. It will change according to file extension it may be either gif or jpg
$type = mysql_result($result,0,"filetype");
//Send the header of the picture we are going to send + cache
header('Cache-Control: private, max-age='.(60*60*24*365));
header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365));
header("Pragma: private");
header('Content-Type: '.$type);
header('Content-Length: ' . strlen($data));
//Send the binary data
echo $data;
}else{
//Not found
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($file_not_found));
readfile($file_not_found);
}
}else{
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($file_not_found));
readfile($file_not_found);
}
?>
Try this..
header("Content-type: image/gif");
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
error_reporting(0);
require_once "class/dbconn.php";
$id=$_GET['id'];
$sql="select thumbimage from image where img_id=$id";
$rs=mysql_query($sql) or die (mysql_error());
$row =mysql_fetch_array($rs,MYSQL_BOTH);
$data = $row[0];
print $data;

php header download redirect

I'm making a file sharing site for the fun. Trying to make it so that when I hit download, it starts the download. Instead of just a link to /files/$file im trying to do a header redirect:
download.php
/**
* File Download
*/
$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);
$file = $row['name'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
Filename: <?=$row['name']?>
Desc: <?=$row['desc']?>
Download this file
Im stuck here, what should I do next?
thank you
Do you mean something like this, where the page will display the information about the file and then when the user has clicked th link it will download it?
<?php
$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);
$file = $row['name'];
if(!file_exists($file))
{
exit('File does not exist');
}
if(intval($_GET['download'])===1)
{
// Generate the server headers
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($file));
}
else
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($file));
}
$data = readfile($file);
exit($data);
}
else
{
?>
Filename: <?php echo $row['name']; ?>
Desc: <?php echo $row['desc']; ?>
Download this file
<?php
}
?>
I'm not following what you are asking. A header redirect looks like:
header("Location: /path/to/new/page");
If you want the existing info to download, looks like you need to hit it with an id in your url:
<a href="file.php?id=<?=$file_id ?>">
where $file_id has the id of the file you are looking for. Personally, I'd put that in another file from the page that shows the listing and download link, but YMMV.
You can only do a redirect before any html has been pushed.
Basically after
Filename: <?=$row['name']?>
Desc: <?=$row['desc']?>
Download this file
is sent to the browser, your chance to perform a redirect with php is over. Maybe a Javascript redirect is more appropriate? Tutorial on Javascript redirects.

Categories