Downloading an SQlite .db file - php

Hello currently I am working on a tool that converts an otherwise file-based stored text array(not really important though what it does). It works great and I am able to download and use it if I download the file off the FTP, but when I try to directly download it to the person using the .php file, it says Invalid file. My current code is:
header('Content-Type: application/octet-stream');
header("Content-Length: " . filesize($dbname));
header('Content-Disposition: attachment; filename="object database.db"');
readfile($dbname);
What is the issue? Is the content-type or something else wrong? Or should I be using readfile(I tried this?)
When I download with the above code, the name of the file seems to be correct or whatever but when I attempt to open it with the SQLite3 Browser(it works when I download directly off the FTP) the program displays "Invalid file format". When I use readfile it downloads as "download" with no file extension and when I attempt to open windows says Invalid file. Any help would be awesome.

There's no application/db content type. You should use application/octet-stream instead. Also filename in Content-Disposition is just filename you suggest client to save file as, but nothing more, so you yet needs to send the file content itself to the remote peer.
readfile(....);
die();
should do the work if your data is stored in the file already, otherwise you can just echo() it instead.

Related

PHP header attachment download corrupts ZIP file

I have a file like "983Y4938920820894838947" on my server, and I'd like the user to save it as "subject.zip".
Using header location makes downloading work and the file is not damaged.
Whenever i use the headers with attachment, content type and the new filename, the download zip file is corrupt (I think?).
Whenever I open the ZIP file (e.g. test.zip), it makes a new file called test.zip.cpgz. I assume this is mac's way of saying the file is corrupt.
I'm using the following code
// Download the ZIP File
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="' . stripslashes($new_filename) . '"');
readfile($filename);
This makes the file corrupt, while the below code works perfectly (but doesn't change the name):
header("Location: $filename");
I tried other headers without any success. Does anybody have any idea? Thanks!
You are missing a header there, if you dont specify the content length, the browser will not know what the size of the file is:
header("Content-Length: {{replace.with.your.file.size}}");
Maybe this will solve your problems.
The problem was that I had an echo. The echo made the zip corrupt. So it makes sense that it only worked the header location and not the regular headers for changing the filename. Removing the echo fixed it for me.

File Download Gives Error "File if broken" on IOS (works on PC)

I have a PHP script which is downloading file after it is created.
Everything is working correctly when using PC but when I want to load same script on my iPad for example I can't download it. I'm getting error that file is broken.
What could be the problem?
My headers in PHP file:
header('Content-Description: File Transfer');
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=offer.doc");
I have a PHP script which is downloading file after it is created.
My bet is you're using HTML to create the file, and giving it a .doc extension to make it look like a Word document.
While this is totally okay and supported by all versions of Word AFAIK, a 3rd party viewer program like on an iPad may be more strict. A HTML based file is technically not a Word document, and the viewer may not be equipped to parse it accordingly.
You may need to resort to generating a real .doc file.

Forcing file to download in CodeIgniter FTP class

I am using the FTP class in CodeIgniter, they have a function for downloading the file from the FTP, however, its only to the server itself. I am trying to get it to download straight to the user.
I know that i could just save it to the server and then force download and then delete. But its a bit of a hassle if the file is large and it would be slow.
So i am wondering from this code, if there is anyway just to use the force_download CI function?
Example;
$this->ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii');
Thanks!
You simply download the file to PHP's standard output stream instead of a file [stream] like so:
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="test.txt"');
$this->ftp->download('/public_html/test.txt', 'php://output', 'ascii');
(Note: headers are used to force the download, otherwise the browser would simply print the contents)
You're welcome!

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.

accessing file of another drive

previously I could download wav file in php and IIS. But now file is not downloadable.I don't know what is going wrong . After installing and changeing php 5.3 to php 5.4 with php manager in IIS, the file is not able to downloaded. I have link to file to download it which looks like this:
Download
download.php scripts
<?php
$filename = $_GET['voice'];
$dir = 'd:/temp_file/voice/';
if(is_file($dir.$filename))
{
header('Content-type: audio/wav');
header('Content-Disposition: attachment; filename='".$filename.'"');
echo file_get_contents($dir.$filename);
}
?>
while I was trying to find the mistake why file is not downloadable, I remove if statement and run program, it does prompt window download file with audio player of empty wav file. So, I conclude that there is mistake in path which is not allowing to access file of another drive. I have php code in c:\inetpub\wwwroot\ but wav file to be download is in the d:\temp_file\voice path. What Should I have to do?
Maybe PHP open_basedir restriction.
Try to add d:/temp_file/voice/, to its content in php.ini.
Update
I never used IIS, but another thing that you have to check is if the IIS User have permission to read that directory. Try to add, just for test, Everyone with all permission to d:\temp_file\voice.
I don't know what is going wrong
Then enable error reporting, for what it looks like to me it's a syntax error at header('Content-Disposition: attachment; filename='".$filename.'"'), you're missing a quote (') after filename='". But since you say it has worked (and works without the if), I'd say that's a copy paste error.
Then do a var_dump($filename), which might contain quotes, since you put those around the URL in the link (<a href="download.php?voice='$filename'">). The file D:/temp_file/voice/'foo.wav' might not exist.

Categories