Forcing file to download in CodeIgniter FTP class - php

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!

Related

Downloading an SQlite .db file

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.

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.

Javascript/Jquery: download multiple images and save locally

I created in php starting with file_get_content to load a HTML page, parse to DOM and using xpath to find all image tags by class name. Essentially in the end I can resize and save all images locally(because I try on localhost).
The thing is once I hosted my php, all images will be saved on the server. The other solution might be to ask the user to install (e.g xampp) to run it locally (but not a preferable solution).
Could I do the same method with javascript/jquery to save all images found in a page? All I know is javascript can save but has to pop-up dialog save as and that also require to be done one by one for each image.
Thanks in advance for any answer.
You can't send multiple individual files. Best bet is to use the zip extension to compress multiple files together and offer that for download.
Zip Extension
You don't need to use JavaScript, you can use PHP to force the image download:
$file = 'path/to/image.png';
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename= ".$file."");
readfile($file);

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