I know there are a lot of mentions of this but I have tried all the suggestions and nothing seems to work.
I have this script to force download files, but when using docx formats it downloads ok but then says the file is corrupt. However word does manage to open it ok.
Does anyone know why the docx would keep saying there corrupt. I have double checked them by ftp them down from the server and they are fine and open first time.
$documentDir = '/home/';
$file = $_GET['d'];
$fileLocation = $documentDir.$file;
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($fileLocation));
header('Content-disposition: attachment; filename="'.$file.'"');
readfile($fileLocation);
exit(0);
This script works for me.
Are you sure that all your documents are situated in /home/ ?
Don't you mean the relative path home/
If this still doesn't work, can you please tell what's the size of the downloaded docx, and open that file in a text editor like sublime_text, the error will probably be written in there.
You could try modifying disposition line and adding encoding line to ensure encoding is done correctly.
header ("Content-Disposition: attachment; filename*=UTF-8'en'$file");
header('Content-Transfer-Encoding: binary');
Related
I have these PHP headers that will force a download (Intranet site) that works on Chrome without prompting the popup, but I cannot avoid the popup warning in Internet Explorer 11 (Version 11.657.18362.0) on Windows 10.
I've tried adding to IE "intranet" site and "internet" site the internal server address this would run from (http://[server]), lower the security levels to the lowest settings for this zone under "intranet" and also "internet". Close and open Internet Explorer and it still has the popup warning.
Here is my PHP code below:
if ($_REQUEST['c']){
$num = $_REQUEST['c'];
$cmd = "test.exe $num";
$ctype ="application/octet-stream";
ob_end_clean();
header('Content-Description: File Transfer');
header('Content-Type:' . $ctype);
header('Content-Disposition: attachment; filename="test.bat"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($cmd));
echo($cmd);
exit;
}
Additionally, once the bat file has downloaded, there is a separate AutoIt script on the user's computer that will scan the download folder for this specific file and will automatically run the file and delete it without the user having to do anything. The idea is to limit the user's clicking to just one time.
This is the popup that I get from IE when I click to download the bat file below:
Any ideas what's wrong or other suggestions around this?
I'm able to reproduce the problem, but it seems that IE browser will keep display this prompt when downloading the bat file for security reason, and we can't disable it.
As a workaround, I suggest you could convert the bat file to a ZIP file, and then download it. Here are some related threads about how to Zip files using JavaScript, you could check them:
How to create zip files using javascript?
How to Zip files using JavaScript?
I have the below code that creates a ZIP file, adds a file to it and then downloads to my computer.
$zip = new ZipArchive();
if ($zip->open('order_sheets.zip', ZipArchive::CREATE) === TRUE){
$zip->addFile($pdfFilePath);
}
$zip->close();
$file_url = 'order_sheets.zip';
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$file_url);
header('Content-Length: ' . filesize($file_url));
readfile($file_url);
All works good but the issue is, when opening the downloaded ZIP, it say's This folder is empty when it is not. If i right click and hit "Extract Here", the contents come out.
Anyone know why that is?
The problem is with Windows' zip utility, which uses IBM850 encoding causing it to misinterpret some characters in internal filenames in the archive, including _ (underscore) as in your file.
See answer here:
PHP ZipArchive Corrupt in Windows
Explained in PHP Manual user notes here: http://php.net/manual/en/ziparchive.addfile.php#95725
I'm using the following code to force download some mp3 files that are stored on my server. This works fine but it takes over 1 minute to download 1 mp3 file, even for a file that is 2.5MB. Something seems wrong for it take that long. Any ideas what I can do to make this download a lot faster?
$fullfilename=$_GET['file'];
$filename=basename($fullfilename);
header('Content-type: audio/mpeg');
header("Content-Disposition: attachment; filename=\"{$filename}\"");
header("Content-Length: " . filesize($filename));
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($fullfilename);
exit;
It depends on the internet connection between the server and your browser. PHP cannot do anything about it.
Now I am developing codeigniter site.
I have one issue.
In order to read&open pdf into web browser.
header("Content-type:application/pdf");
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Cache-Control: max-age=0'); //no cache
header("Content-Disposition:attachment;filename=\"".$file."\"");
readfile($filePath);
above code $file is filename and $filePath is pdf file path.
When it runs on local server, $filePath is value such as "http://localhost/.pdf" and it runs well.
But when runs on hosting server, this value is "http://.com/***.pdf"
And doesn't run.
We can not open with pdf format error.
file content didn't include readed.
I know that is cause of URL issue.
But I have no issue!
Your Content-Disposition should be inline if you want to display the file in the browser and not as a download but I guess it doesn't really matter if you can get it to work.
If your allow_url_fopen config in PHP is set to Off, you will not be able to read URL file from within your PHP script.
Anyway, your code should look something like this.
<?php
$file = "lesson2.pdf";
$filePath = "http://kmmc.in/wp-content/uploads/2014/01/lesson2.pdf";
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Content-Disposition: inline; filename=\"" . $file . "\"");
readfile($filePath);
I'm in the middle of developing a Safari extension for imageboard-type websites and one of the bigger features I'm hoping to implement is the ability to download all of the images (the posted ones, not the global page-level images) that had been posted.
There are similar questions here already, but mine differs a bit in that the images in question are hosted on an entirely different server. I've been brainstorming a bit and figured that gathering all of the image URLs in a JS array then sending it to my server to be turned into a zip file (forcing the download, not just a link to the file) would be the best way to go. I also want the zip to be deleted after the user downloads it.
I've already finished the majority of the extension features but this one is stumping me. Any help would be greatly appreciated.
How would I'd go about doing this?
You want a extension to contact your server for downloads? That's a terrible idea! Make the zipfile locally - it's not regular javascript, it's an extension - you have full access.
Anyway assuming you want to do this anyway, what is the trouble you are having? You get a list of urls, send them to your server, your server downloads them, zips them and send them to the user. (The "your server downloads them" part should worry you!)
What problem are you having?
You can use PHP's ZipArchive class to make a ZIP, then stream it to the browser.
<?php
// Create temp zip file
$zip = new ZipArchive;
$temp = tempnam(sys_get_temp_dir(), 'zip');
$zip->open($temp);
// Add files
$zip->addFromString('file.jpg', file_get_contents('http://path/to/file.jpg'));
$zip->addFile('/this/is/my/file.txt');
// Write temp file
$zip->close();
// Stream file to browser
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=myFile.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($temp));
readfile($temp);
unlink($temp);
exit;