PHP file download with no changes to the file - php

I am trying to make a file download by using PHP but the file downloaded is a clean mp3 file without tags/metadata and not the one that is on my server.
Here's a detailed explanation:
I have a mp3 file with saved ID3 tags and all information on my server.
I run this code to start the download:
header('Content-Type: application/mp3');
header('Content-Disposition: attachment; filename="file.mp3"');
header('Pragma: no-cache');
readfile("file.mp3");
This starts a file download, but the file that is downloaded loses all its meta data, including any info and album art. Is there a way around this?
For example opening the file url and right clicking -> Save As, downloads it and preserves all the information stored in the file.
How can I prevent the deletion of all the metadata? Thanks for your help

Just add these two lines:
ob_end_clean();
flush();
readfile("file.mp3");

Related

How to use multiple http headers?

I have a php page which downloads zip files. Normally after the download, it automatically revert back the user to a previous page(myfiles.php) using header('location:myfiles.php');.
When I execute the page, it bring me to myfiles.php but the download pop up won't show up, thus preventing me to download my zip file. When I remove the line header('location:myfiles.php');, I am able to download my zip file as expected.
Below is an extract of my code.
//Some codes
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
session_start();
$_SESSION['correct']="Files downloaded sucessfully";
header('location:myfiles.php');
Can you please help me finding a way to fix it? Thank you.
Think about what those headers are doing
These 2 and the readfile
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
are sending a file to the current page that is on the browser.
This one, which will run as well as the code above
header('location:myfiles.php');
attempts to tell the browser to go to another page. If it did do that then the file you sent to the browser would just disappear
So basically done together as one flow, they do not make sense!
Also you cannot send a header('location:...) after any actual data has been sent to the browser, which of course you did when you ran the readfile($zip_name);

PHP File creation and download on the fly

Hey guys I was wondering how I could download a file that is generated on the fly by PHP. The file I want to download would be an XML file. At the moment all my code does is create a long string with all of the data that is to put in the file, it then simply writes the string to a file and saves it with a .XML extension. This is currently working in my local machine using a copy of the website, it won't work on the web server though due to read/write permissions.
So is there a way to generate a file in the fly to be immediately downloaded without storing it on the web server?
If the same script is generating the file, you could echo or print the contents on to the page and use header to force download.
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
echo $XMLString;
?>
And if you have a different file for downloading the file, just use file_get_contents and output the file data!
That should do you :)
Just give these two things on the top of the document:
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="myxml.xml"');
?>

Opening downloaded zip file creates cpgz file?

If I make the url for a zip file the href of a link and click the link, my zip file gets downloaded and opening it gets the contents as I expect.
Here's that HTML:
download zip
The problem is I'd like the link to point to my application such that I could determine whether the user is authorized to access this zip file.
so I'd like my HTML to be this:
download zip
and my PHP for the /canDownload page:
//business logic to determine if user can download
if($yesCanDownload){
$archive='https://mysite.com/uploads/my-archive.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archive));
header("Content-Length: ".filesize($archive));
ob_clean();
flush();
echo readfile("$archive");
}
So, I think the problem has to do with the header() code but i've tried a bunch of things related to that based on various google and other SO suggestions and none work.
If you answer my question, it is likely you can answer this question too: Zipped file with PHP results in cpgz file after extraction
The answer in my case was that there was an empty line being output before readfile().
So i added:
ob_end_clean();
readfile($filename);
But you should probably search for the place where this line is being output in your code.
The PHP documentation for readfile says that it will output the contents of a file and return an int.
So your code, echo readfile("$archive");, will echo $archive (btw, the double quotes are meaningless here; you should remove them), and THEN output the int that is being returned. That is, your line should be: readfile($archive);
Also, you should be using a local path (not an http:// link) to the archive.
Altogether:
if($yesCanDownload){
$archive='/path/to/my-archive.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archive));
header("Content-Length: ".filesize($archive));
ob_clean();
flush();
readfile($archive);
}
Lastly, if that does not work, make sure filesize($archive) is returning the accurate length of the file.
Ok, I answered my own question.
The main problem, which I originally didn't make clear, was that the file was not located on my application server. It was in a Amazon AWS s3 bucket. That is why I had used a full url in my question, http://mysite... and not just a file path on the server. As it turns out fopen() can open urls (all s3 bucket "objects", a.k.a. files, have urls) so that is what I did.
Here's my final code:
$zip= "http://mysite.com/uploads/my-archive.zip"; // my Amazon AWS s3 url
header("Content-Type: archive/zip"); // works with "application/zip" too
header("Content-Disposition: attachment; filename='my-archive.zip"); // what you want to call the downloaded zip file, can be different from what is in the s3 bucket
$zip = fopen($zip,"r"); // open the zip file
echo fpassthru($zip); // deliver the zip file
exit(); //non-essential
Another possible answer, I found After much searching, I found that the two possible reasons for a *.zip "unzipping" to a *.zip.cpgz are:
the *.zip file is corrupted
the "unzip" tool being used can't
handle >2GB files
Being a Mac user, the second reason was the cause for my problem unzipping the file: the standard Mac OS tool is Archive Utility, and it apparently can't handle >2GB files. (The file in question for me was a zipped 4GB raspbian disk image.)
What I ended up doing was to use a Debian virtual machine, already existing in Virtual Box on my Mac. unzip 6.0 on Debian 8.2 had no problem unzipping the archive.
You're passing the URL to readfile() like:
$archive = 'https://mysite.com/uploads/my-archive.zip';
While you should pass the path on the server, for example:
$archive = '/uploads/my-archive.zip';
Assuming the file is located in the upload folder.
Additionally try the following headers:
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=file.zip");
In my case, I was trying to create the file in a directory above public_html and the rules of the hosting didn't allow it.

.exe File becomes corrupted when downloaded from server

Firstly: I'm a lowly web designer who knows just enough PHP to be dangerous and just enough about server administration to be, well, nothing. I probably won't understand you unless you're very clear!
The setup: I've set up a website where the client uploads files to a specific directory, and those files are made available, through php, for download by users. The files are generally executable files over 50MB. The client does not want them zipped, as they feel their users aren't savvy enough to unzip them. I'm using the php below to force a download dialogue box and hide the directory where the files are located.
It's Linux server, if that makes a difference.
The problem: There is a certain file that becomes corrupt after the user tries to download it. It is an executable file, but when it's clicked on, a blank DOS window opens up. The original file, prior to download opens perfectly. There are several other similar files that go through the same exact download procedure, and all of those work just fine.
Things I've tried: I've tried uploading the file zipped, then unzipping it on the server to make sure it wasn't becoming corrupt during upload, and no luck.
I've also compared the binary code of the original file to the downloaded file that doesn't work, and they're exactly the same (so the php isn't accidentally inserting anything extra into the file).
Could it be an issue with the headers in my downloadFile function? I really am not sure how to troubleshoot this one…
This is the download php, if it's relevant ($filenamereplace is defined elsewhere):
downloadFile("../DIRECTORY/files/$filenamereplace","$filenamereplace");
function downloadFile($file,$filename){
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
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));
# flush();
readfile($file);
exit;
}
}
ETA Additonal Info:
- Tests for working/non-working files have been done on the same machine
- If it makes any difference, the original file has a custom icon. After download, the file has a generic blank document icon.
Additonal Info: I THINK THIS ONE'S IMPORTANT!
I just tried downloading the file directly (to bypass the download link that triggers the download function above). If I download the file by just going to its url and downloading it that way, the downloaded file WORKS. So I'm thinking it must have something to do with the download function. But what??
3/17 MAJOR CORRECTION —AND RESOLVED—
So I woke up this morning and it dawned on me that maybe I was comparing the files wrong. (I had re-saved them as binary text, and then compared them. I didn't realize the comparison program would take and compare actual exe files). This morning I tried comparing the actual exe files and there is a difference. There was one line of php code that was being injected into the first line of the file. I adjusted the php, and the problem was fixed. (It was from the if/else statement that defined teh $filenamereplace variable in the code I'd cited). Thanks again for all your help, and sorry for misleading you in insisting that the files' contents were identical!
"I've also compared the binary code of the original file to the downloaded file that doesn't work, and their exactly the same (so the php isn't accidentally inserting anything extra into the file)."
If that's really true, then the problem must be in how the exe is started after it has been downloaded. It should certainly not be a problem with your PHP code.
Perhaps they were corrupted on upload. This can happen if you transfer them via FTP in ASCII mode instead of BINARY.

Forcing a download using PHP

I have PHP issue, I'm trying to force a file download using php
if users click on my link www.site.com/download.php it redirects them to download.php with the following code inside the download.php file
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=custom_report.csv');
header('Pragma: no-cache');
echo readfile('files/csv/custom_report.csv');
readfile and fopen however is disabled on the server for security purposes.
Is there any other way I can do this
How about file_get_contents()?
If readfile and fopen aren't available, I'd expect all the file I/O fns to be disabled too.
In which case your only option is to provide a hard link to the csv file (but your service provider probably hasn't added the CSV mime mappings)
C.
From what I understand your problem is not getting the file to download but rather finding an alternative to readfile/fopen?

Categories