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?
Related
I'm aplying this link in order to develop an easy script to download an Android .apk file. But instead of download file, is downloading itself.
https://stackoverflow.com/a/22605604
Here is my code:
<?php
$filename = shell_exec('ls /var/www/html/app/*.apk');
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile($filename);
exit();
?>
Php.info is rendered correctly by Apache.
Thanks.
Your path is not set correctly!
Try to use a real path/filename:
$filename = "/var/www/html/app/nameofyourfile.apk";
And also your content-type is not set correctly, try:
Content-type: application/com.android.package-install
If these two errors are corrected, it would probably force to download your apk file.
Anyway, this should work ->
<?php
$filename = "/var/www/html/app/yourfile.apk";
$contenttype = "application/com.android.package-install";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=$filename);
readfile($filename);
?>
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 need some eduction please.
At the end of each month, I want to download some data from my webserver to my local PC.
So, I've written a little script for that, which selects the data from the DB.
Next, I want to download it.
I've tried this:
$file=$month . '.txt';
$handle=fopen($file, "w");
header("Content-Type: application/text");
header("Content-Disposition: attachment, filename=" . $month . '.txt');
while ($row=mysql_fetch_array($res))
{
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
If I run this, then the file is created, but my file doesn't contain the data that I want. Instead I get a dump from the HTML-file in my browser..
What am I doing wrong..
Thanks,
Xpoes
Below script will help you download the file created
//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
Your current code does not output a file, it just sends headers.
in order for your script to work add the following code after your fclose statement.
$data = file_get_contents($file);
echo $data;
The code below forces browser to fire a prompt to save/open file (https://kb.wisc.edu/images/group27/13334/open-prompt.PNG) even if it's a image or pdf file.
I want images to be opened as usual, pdf files to be displayed in browser. And of course other files that are not supported by browser like zip, rar, doc, xls etc will fire a save file dialog.
Edit:
My intention is not to block client to save the file of course they can save it that's impossible. I want to serve let say images as PHP files like main.php?file=randomcode (which is stored in database) but not as /images/somefilename.jpg . My code forces client to download it but I want to display it.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename . "." . $fileinfo["file_extension"] . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
$fp = fopen($file, "r");
if ($fp) {
while (!feof($fp)) {
$cur_data = fread($fp, 1024);
echo $cur_data;
}
} else {
echo "Error: Could not the read file.";
}
Ultimately it's up to the client what to do with the content it receives. One thing you can do is get rid of the Content-disposition header:
header("Content-Disposition: attachment; filename=\"" . $filename . "." . $fileinfo["file_extension"] . "\";");
(Or at least get rid of it conditionally, depending on specific factors about the file.) What this header does is tell the client that the content being returned is a "file" (you even provide a suggested name for the file) and should be treated as such. HTTP has no native concept of "files" so this header exists specifically to identify something as a "file."
By not supplying that header, you're not suggesting to the client that the content is a file. The client may still infer that it's a file and treat it as such (which you can't control), but from your end all you'd be doing is returning the content itself.
Well, apparently you know the file extension so you could do:
if(in_array($fileinfo["file_extension"], array('jpg', 'png', 'gif')) {
// set header for viewing the image
$mime_type = $fileinfo["file_extension"];
if($mime_type == 'jpg') {
$mime_type = 'jpeg';
}
header('Content-Type: image/' . $mime_type);
}
else {
// set headers for downloading the file
}
if the content type is set to octate stream then it will defenetly transfer the file means user will force download it. you have to set content type accordingly to open it in browser
for example if type is image then
header("Content-Type: image/jpg");
header("Content-Type: image/png");
etc.
and if its image or pdf then remove Content-Disposition: header
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...