i have a problem in my code php
i make wallpaper script
After upload image the name of image is saved in the database and image saved in the file (images/)
i want after show image in page php and click download
i want to download image in desktop
but after click in download the image saved in desktop but But be empty
see this image : http://im51.gulfup.com/riod8Y.png
my code
<?php
$getpic = $db->query('SELECT *','wallpaper',$where = array(
'id',
'=',
"".$id.""
));
$row = mysql_fetch_object($getpic);
$file = 'images/'.$row->image;
if($_POST['download']){
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
?>
Related
I tried to create some code so that a user can download an image in laravel without right clicking and using Save as... but after I created this code and download the image, trying to open the image says the image was damage.
$file = urldecode($request->file);
$remoteURL = $base_url.$file;
//$fname = basename($remoteURL);
// echo $remoteURL;
// exit;
// header('Content-type: image/*');
header('Cache-Control: must-revalidate');
header("Content-Type: application/octet-stream");
header('Expires: 0');
// header('Content-Disposition: attachment; filename='.basename($remoteURL));
header('Content-Disposition: attachment; filename="'.basename($remoteURL).'"');
// header('Content-Length: '. filesize($remoteURL));
header('Pragma: public');
flush();
readfile($remoteURL);
// file_get_contents($remoteURL);
exit;
I want to offer a PNG image to download only for some users, and the image should not be located anywhere, so the users should not see it's path.
That's why I inserted it to the download.php as a base64 image. The problem is, however the image is offered for download, but it shows 0 B size when downloaded.
$base64strImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxoAAARjCAIAAABnqMWCAAAACXBIWXM...';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myimage.png');
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(base64_decode($base64strImg));
exit;
Not sure where is the problem, if it can't handle big image or why it can't be downloaded.
I also tried this way:
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: application/force-download');
echo base64_decode($base64strImg);
Now the image has correct size, but still, can't be opened after download. The image viewer says unsupported format.
And third option - without decoding, it also has a correct size, but still can't be opened.
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=test.png');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($base64strImg));
ob_clean();
flush();
echo $base64strImg;
exit;
According to advice from #Lawrence I updated my code and this way it works:
$resultimg = str_replace("data:image/png;base64,","",$base64strImg);
header('Content-Disposition: attachment;filename="test.png"');
header('Content-Type: image/png');
echo base64_decode($resultimg);
you don't have to do all that you can just upload image and restrict access to it by htaccess or permissions and use readfile with the headers in download.php and check if the user has the permission to download the file .
I can't download files that already uploaded in my website, for example :
I already uploaded a video file with 800mb file size and it is okay, the file is save to my directory in the File folder, then I want to download that file again, but as soon as I download the file, I will always got 1kb file not the exact size of the file, and when I play it, nothing happen
this is my code:
<?php
include 'db.php';
if(isset($_REQUEST['name']))
{
$var =$_REQUEST['name'];
$dir = "../files/";
$file = $dir . $var;
if(file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: video');
header('Content-Disposition: attachment;
filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
mysqli_close($conn);
exit();
}
else{
echo "File not found";
}
}
?>
Yeah, I had the same problem too. After searching alot on the internet, I found out that the problem is related to output buffering. The following code solved my problem.
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_end_clean(); //adding this line solves my problem
readfile($file);
exit;
?>
The code ob_end_clean() basically runs grabs everything in the buffer, then erases the buffer, and turns off output buffering.
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
I'm working on Yii and I want to upload and download some files, I can upload some files to the Documents table in the blob_document field which is blob type, now I've tried doing this to download the files
<?
$file = Documents::model()->findByPk('3');
$file = $file->blob_document;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $file;
exit;
?>
I'm downloading a .txt file which is the correct extension but the content has the name of the file instead of the original text.
$file->blob_document is only the filename, to get the content, you can use file_get_contents, or better (less memory consumption), readfile() to output the contents of the file.
<?php
$file = Documents::model()->findByPk('3');
$file = $file->blob_document;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($file);
exit;
?>
What does the content look like inside the database? Perhaps it is not being inserted properly....
Abstracting SQL, should be a crime ;-)