The code below displays this:
/home/my_site/www/wp-content/uploads/2017/03/2017-03-17-my_file.mp3
As far as I can tell, this path is correct. Yet when I comment away the echo and download the file, it's an empty file. Why?
Code:
if (isset($_GET['file'])) {
clearstatcache();
$file_path = str_replace('http://www.example.com/', '', $_GET['file']);
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $file_path . '.mp3';
echo $file_path;
if(file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$file_size);
header("Content-Disposition: attachment; filename=".$file_name);
exit();
}
else {
die('The provided file path is not valid.');
}
}
EDIT: this is what I have after KIKO Software's suggestion. Still downloads empty file.
<?php
if (isset($_GET['file'])) {
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['file'] . '.mp3';
//echo $file_path;
//$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
//readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
}
?>
Simple example to download a file:
$content = 'This is the content.';
$size = strlen($content);
$name = 'test.txt';
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$name);
echo $content;
Make sure this works first. If it doesn't there could be a problem with the headers. Switch on error reporting and see: How to fix "Headers already sent" error in PHP
Then build up on that. Start by downloading the script itself:
$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
And only after that try to download something else.
By approaching the problem step by step it is easier to see where it goes wrong.
Related
I had a problem with my code . when I click the button to download the picture , it appears the code that I do not know, how to solve this ? what is wrong with my code ?
--> Download.php
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
} else {
$filename = NULL;
}
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = '../images/upload/lowongan_pekerjaan/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error message if file can't be opened
$file = #fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
why show like this?
My Picture
whats wrong with my code?
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
should be
header('Content-Type: image/jpg');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
or png or gif or whatever your file type is.. the browser does not know how to handle what you are sending it because application/octet-stream != an image type.
I am trying to download image file in CI but getting error when I open download image file. Need help :(
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
$this->load->helper('download');
$data = file_get_contents($file_path); // Read the file's contents
$name = 'ups_label.png';
force_download($name, $data);
}
else
{
echo "file does not exist";
}
GOD. Found out the solution :)
if(!file)
{
File doesn't exist, output error
die('file not found');
}
else
{
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');
ob_clean();
flush();
readfile($file);
exit;
}
Use header to force download. Use the code below
$file_name = $_GET['file_name'];
$file_path = "./ups_printimages/".$file_name;
if(file_exists($file_path))
{
header('Content-Type: image/jpeg');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_name) . "\"");
readfile($file_path);
}
else
{
echo "file does not exist";
}
Hope this helps yoiu
function download($data = false){
$file_name = '2013-07-01-093847test';
$extension = 'txt';
$filename = $file_name.".".$extension;
$upload_folder='path\to\folder\';
$uploadDir = WWW_ROOT.$upload_folder;
$file=$uploadDir.'\\'.$file_name;
$new_file=$uploadDir.'\\'.$filename;
copy($file, $new_file);
$ext = end(explode('.',$new_file));
if($ext && array_key_exists($ext,$file_types)){
if(file_exists($new_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/stream');
header('Content-Disposition: attachment; filename='.basename($new_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 78000');
header('Cache-Control: private');
header('Pragma: public');
header('Content-Length: ' . filesize($new_file));
#readfile($new_file);
unlink($new_file);
exit;
}
}
else {
die('The provided file path is not valid.');
}
}
While I am trying to download a file using the above code from an extjs interface, the file content gets displayed at the firebug console part and not getting download to the client machine?
I am uploading file by storing its path in mysql in that way:
if(isset($_FILES['filefield']))
{
$file = $_FILES['filefield'];
$file_name = $file['name'];
$file_size = $file['size'];
$file_type = $file['type'];
$file_tmpname = $file['tmp_name'];
$upload_dir = "C:/xampp/htdocs/intranet/www/public/uploads/";
$ext_str = "gif,jpg,jpeg,mp3,tiff,bmp,doc,docx,ppt,pptx,txt,pdf";
$allowed_extensions=explode(',',$ext_str);
$max_file_size = 10485760;
$ext = substr($file['name'], strrpos($file['name'], '.') + 1);
if (!in_array($ext, $allowed_extensions))
echo "only".$ext_str." files allowed to upload";
if($file['size']>=$max_file_size)
echo "only the file less than ".$max_file_size."mb allowed to upload";
$path = $upload_dir.$file_name;
if(move_uploaded_file($file_tmpname, $upload_dir.$file_name))
ORM::factory('files')->uploadFile($teacher_id, $file_name, $file_size, $file_type, $path);
else
echo "The file cant moved to target directory.";
}
How can I then download this file by its path? If my upload code is correct, of course.
Try this
$file = 'C:/xampp/htdocs/intranet/www/public/uploads/'.$file_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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
I have a script that creates a zip files from files in a certain directory. After Download, for a lot of users - the zip file is empty. However, for other users - the file isn't empty. Is it something I'm doing wrong?
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$id.'.zip"');
header('Cache-Control: private');
ob_clean();
flush();
readfile("".$id.".zip");
exit;
Better add some error control:
$file = "{$id}.zip";
if (!is_readable($file))
{
die('Not accessible.');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Cache-Control: private');
ob_clean();
flush();
readfile($file);
exit;
Additionally triple check your output buffering configuration vs. readfile.
I recommended this as a suggestion earlier, here is a skeleton of what I am talking about. It is untested as I don't have access to php at the moment.
$filename = $id . '.zip';
$handle = fopen($filename, 'r');
if ($handle === false)
{
die('Could not read file "' . $filename . '"');
}
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: private');
while (!feof($handle))
{
echo fread($handle, 8192);
}
fclose($handle);
exit;