So I have files stored on Amazon S3. My customers download these files from our website, they click download and it sends the info to our download.php page (the customers don't see this page), but there it uses PHP to get the file name and path (code below). But the issue we have is that it's not telling the browser the file size, so when the customer is downloading they see "remaining time unknown". How can I make it so that the download.php page can get that information and pass it alone?
<?php
$file_path = "http://subliminalsuccess.s3.amazonaws.com/";
$file_name = $_GET['download'];
$file = file_get_contents('$file_name');
header('application/force-download');
header( 'Content-Type: application/octet-stream' );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
$pos = strpos($file_name, "http");
if ($pos !== false && $pos == 0)
{
readfile($file_name);
} else readfile($file_path.$file_name);
?>
That's very easy. Look, when you do file_get_contents(), you can get your file size with strlen(). Then you send Content-Length header in the response.
<?php
$file_path = 'http://subliminalsuccess.s3.amazonaws.com/';
$file = trim($_GET['download']);
$file_name = $file_path.$file;
$file_contents = file_get_contents($file_name)
OR die('Cannot get the file: '.$file);
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Length: '.strlen($file_contents));
header('Content-Disposition: attachment; filename="'.basename($file).'"');
echo $file;
BTW, there're so many mistakes in your code. For example, you read files twice, one time with file_get_contents() and second time with readfile(). $file_name variable doesn't have URI. file_get_contents('$file_name') is also wrong.
Also, you don't check your incoming URLs, but just readfile() it which is not nice as someone might pass any URLs to your script...
Related
I just want to export my live sql data into excel file inside specific directory. I am able to to download the excel file from sql but it is downloading in download folder in sytem but i want to download it on live server "/home/jw07sp1ptrfw/public_html/" this is my directory on live server.But when I am running this code its downloading the file home_jw07sp1ptrfw_public_html_report.xls in system download folder. Please help me if any one can thanks in advance
include('config.php');
$html='<table> <tr><td> Name</td><td>Email</td><td>Phone</td></tr>';
$result = mysqli_query($link,"SELECT * FROM `order`");
while($row = mysqli_fetch_assoc($result))
{
$html.='<tr><td>'.$row["name"].'</td><td>'.$row["email"].' </td><td> '.$row["phone"].'</td></tr>';
}
$html.='</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename= /home/jw07sp1ptrfw/public_html/report.xls');
echo $html;
I have also tried this. But not getting the solution.
$file_name = "report.xls";
$rootDir = realpath("/home/jw07sp1ptrfw/public_html");
$fullPath = realpath($rootDir . "/" . $file_name);
// if ($fullPath && is_readable($fullPath) && dirname($fullPath) === $rootDir)
{
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=' . basename($fullPath));
readfile($fullPath);
echo $html;
}
You can't do that. The filename is just a hint to the browser, and it will preprocess it for security reasons. See https://greenbytes.de/tech/webdav/rfc6266.html#disposition.parameter.filename.
I have an upload php script, that uploads a file to my server with a random name (without extension)
<?php header('Access-Control-Allow-Origin: *');
//Read parameters
$documentuniqueid = addslashes($_REQUEST['did']);
//Get file name
$filename = urldecode($_FILES["file"]["name"]);
//Move file to folder
move_uploaded_file($_FILES["file"]["tmp_name"], "docs/".$documentuniqueid);
?>
I can upload an image from my iPhone without any errors (via a PhoneGap app, using the FileTransfer plugin), but when I download that file to my Desktop, add the ".jpg" extension to it, the image will be corrupted (with random vertical lines in it)
Also, for downloading, I use the following PHP script:
<?php
...reading parameters...
$original_filename = "http://www.example.com/docs/".$document_id;
$new_filename = $real_document_name;
//Complex header (NOT USED, but I also tried this, where the content-type is specified exactly for JPGs
//headers to send your file
//header("Content-Type: application/jpeg");
//header("Content-Length: " . filesize($original_filename));
//header('Content-Disposition: attachment; filename="' . $new_filename . '"');
//Second one
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: '.finfo_file($finfo, $original_filename));
header("Content-Length: " . filesize($original_filename));
header('Content-disposition: attachment; filename="'.$new_filename.'"');
//clean up
ob_clean();
flush();
readfile($original_filename);
exit();
?>
It works fine for files that are ~700 KBs, but for files from my iPhone which are 4MB, it's not working anymore.
Should I specify a max file size somewhere?
I have seen many examples but none of them is resolving my issue.
I generated cvs file in ajax post request ( I am not changing window.location.href). I want this file to auto download just like what happens after changing window.location.href. Currently i don't know solution.Kindly help me here is my code
$file_name="temp_".time().".csv";
$new_csv = fopen($file_name, 'w');
fputcsv($new_csv, $csv_data);
fclose($new_csv);
header("Content-type: application/csv; charset=utf-8");
header("Content-disposition: attachment; filename =\"" .$file_name. "\"");
readfile($file_name);
unlink($file_name);
exit;
$this->setLayout(false);
return sfView::NONE;
I'm creating a website on my localhost that should let people download some .rar files.
In my index I've created some tags like this:
$filename = "Test001.rar";
'.$filename.'';
This is just an example of one single file, but in my php file 'download.php' I've got the problem when I want to download the .rar file
This is download.php
<?php
echo "Welcome to Knowledge!";
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file'])
{
$file = $_GET["file"];
$path = 'C:\xampp\htdocs\TestSite'."\\".$file;
}
$err = $path.'Sorry, the file you are requesting doesnt exist.';
if (file_exists($path) && is_readable($path))
{
//get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/x-rar-compressed, application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
readfile($filename);
}
?>
It opens the stream in the right way, but I get that the file size is about 200 bytes and not the full length that is about 200MB.
How can I fix this problem?
Remove the echo statement, there should not be any output before the headers. Change readfile($filename) to readfile($file)
I am using the following function to download pdf file it is working fine when i download it from PC or laptop but when i click on download link using ipad it opens a page with lots of special chracters and I am unable to download the file.
My download function is
public function download() {
$download_path = $_SERVER['DOCUMENT_ROOT'] . "/import";
$filename = $_GET['file'];
$file = str_replace("..", "", $filename);
$file = "$download_path/$file";
if (!file_exists($file))
die("Sorry, the file doesn't seem to exist.");
$type = filetype($file);
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header('Pragma: no-cache');
header('Expires: 0');
readfile($file);
}
Any idea about this error ?
This is probably the issue:
$type = filetype($file);
header("Content-type: $type");
From the manual
Possible values are fifo, char, dir, block, link, file, socket and
unknown.
Which are not things you want to see in the header. You are probably looking for:
header('Content-type: application/pdf');
You probably want finfo_file() and not filetype().