The following is part of the code in upload.php of OpenCart:
$file = $filename . '.' . md5(mt_rand());
move_uploaded_file($this->request->files['file']['tmp_name'], DIR_UPLOAD . $file);
// Hide the uploaded file name so people can not link to it directly.
$this->load->model('tool/upload');
$json['code'] = $this->model_tool_upload->addUpload($filename, $file);
For the coding '$this->model_tool_upload->addUpload($filename, $file)', it will insert the data like this:
The field 'code' is generated by the the coding 'sha1(uniqid(mt_rand(), true))'.
And the new file will inserted into the folder:
I would like to upload image and then display it. In this situation, how can I get the file path correctly using PHP?
Also, can someone explain why the filename is appended with the md5 string? Is it only used for hide the uploaded file name and what is the field 'code' used for?
UPDATE
Just find the answer in one of the source file:
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="' . ($mask ? $mask : 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));
readfile($file, 'rb');
Related
I'm using this code to force a download using headers, the file name is renamed correctly but the file is empty, the actual file isn't being downloaded.
$file = url . '/uploads/audio/' . $trackFile;
$tmp = explode(".", $file);
$newTrackName = $trackTitle . '.' . end($tmp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $newTrackName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
You are missing the actual sending of the file e.g.
echo readfile($file);
exit;
or
header("Location: $file");
You need to add..
readfile($file);
exit;
You are correct and I upvoted both answers but I also found that the new version of cpanel turned of fopen by default, I had to enable fopen.
I'm trying to download pdf/jpg/jpeg/png using PHP. Here is my code
$result= mysqli_query("SELECT * FROM upload WHERE upload_id='$id'");
$row = mysqli_fetch_array($result);
//get file extension
$filename = $row['upload_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if($ext=='jpg'||$ext=='jpeg'||$ext=='png' ||$ext=='pdf'){
echo "Download";
}
download.php
$name= $_GET['id'];
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("uploads/".$name); //showing the path to the server where the file is to be download
exit;
When I tried the above code, it can be download however, when I tried opening the pdf/jpg/png it says invalid. Is there anything wrong with my code? This is my first time using the download code. So I really dont know what is the problem.
And for this path as stated in the code below,
readfile("uploads/".$name);
How do I write it if my pdf/jpg/png files are saved here in my computer C:\Apache2.2\htdocs\epals\uploads
Please help thanks
This question already has answers here:
Proper MIME media type for PDF files
(3 answers)
Closed 8 years ago.
I am using mongodb with php. I have agreement download option on which when user clicks user get the agreemment downloaded. On click i have navigated to script where file download code in php is there
code is
$mongoDbA = $mongoDb->findOne(array("agency_id" => new MongoId($_GET['id'])));
$filename = '../../images/upload/'.$mongoDbA['file'];
if (file_exists($filename))
{
header('Content-Description: File Transfer');
header('Content-Type: text/pdf');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
}
I have allowed to upload only pdf files so only pdf files are there in database
When i click then it gives me error & gives me white colored file that has no extension & size is 1 kb hardly....what is happening???
$filename = './images/upload/'.$mongoDbA['file'];
$fileinfo = pathinfo($filename);
$sendname = $fileinfo['filename'] . '.' . $fileinfo['extension'];
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="$sendname"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
I've got a web from that has two buttons for submitting, one sends an email with pdf attached, this works perfectly.
The second button is to download the pdf, this is the problem. I am saving the pdf in a temp file before download but after it is downloaded the file doesn't open and it is corrupt. The pdf is about 30KB. I have tried solutions to similar questions but always the same result, the pdf won't open.
This didn't work
$fileName = "file.pdf";
$file_name = ("temp/file.pdf");
file_put_contents($file_name, $pdf_content);
$filepath=$file_name; //file location
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
This didn't work
$path = $_SERVER['DOCUMENT_ROOT']."/temp/"; // change the path to fit your websites document structure
$fullPath = $path.$fileName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment;
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;
This didn't work
set_time_limit(0); // disable timeout
$file = $root_path.'/full-tile-book.pdf';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="NewName.pdf"');
header('Content-Length: ' . filesize($file));
$f = fopen($file, 'rb');
fpassthru($f);
fclose($f);
exit;
This didn't work
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filepath)) . ' GMT');
header('Content-disposition: attachment; filename=file.pdf');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filepath)); // provide file size
header('Connection: close');
readfile($filepath);
exit();
The file is always in the temp folder on the server and that works fine so somewhere in the download the file is getting corrupted.
I don't care how the download is done, pdf or oclet-stream or any other way.
I removed the : Content-Type header and it works for me.
Regards
Hi I am trying to allow download a demo file which is related to a product. my code is like this
if(isset($_POST['submit'])){
$root = $_SERVER['DOCUMENT_ROOT'];
$path = "/download/";
$path = $root.$path;
$filename = $demo;
$file = $path.$filename;
header('Content-Type: application/force-download');
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Length: ".filesize($file));
readfile($file);
}
I am able to get file name which is associated with the product by
$demo
After user submits his information, download will start automatically. Now this is downloading a non existing file or corrupted file with the proper file name. please help
<?php
$file = 'monkey.gif';
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;
}
?>
As you can see Content type is application/octet-steam meaning file is byte by byte encoded. Also the cache headers are set. Then headers are forcefully sent by ob_clean();flush(); and then the file is read.
The file_exists is there to ensure that given file exists. You should also try not not thrust user input as they could easy write names for your php codes and download EACH file. And with ../ in names of the files, even your documents or system files and so on.