php get file from ftp server and display save file dialog - php

I have a php page running on an apache server and a ftp server that stores the files, on a different machine. The ftp server is not accessible from outside.
I would like that when the user clicks a button on my webpage, the php server connects to the ftp server, retrieves a file, and displays a save as dialog, so that the user can store it.
Any help is appreciated,
Radu

Here you Go.. in download.php write this
$file = 'location/of/your/file.ext';
readfile($file);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$file);
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
This should work :)

Related

Download Excel file with PHP over https

I am about to take a web application I've been working on live and am now, of course, updating code to make things that worked perfectly on my localhost work on the secure server. One of the tasks this application performs is generating and downloading files. All of the files are being generated and written to the server, but the .xls, .csv, and .json files download with no data even though the data is in the files on the server. The .xml, .zip, and .sql files do download with accurate data. An .xls or .json file that is contained within a .zip file is fine. This problem is not IE specific as I am using Chrome. Also tried Safari with the same results.
Here is what I'm doing to download the Excel:
$excel_content = file_get_contents($download_file_name);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file_name);
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
print chr(255).chr(254).mb_convert_encoding($excel_content, 'UTF-16LE', 'UTF-8');
Here is what I'm doing to download the XML:
$xml_content = file_get_contents($download_file_name);
header("Content-Type: application/xml");
header("Content-Disposition: attachment; filename=\"".$file_name);
header("Content-Transfer-Encoding: UTF-8");
header("Pragma: no-cache");
header("Expires: 0");
print $xml_content;
Can anyone see why the Excel works on my localhost, but not on my server through https protocol? If it matters, I do have openssl enabled as verified with an info.php file on the server.

Download different file to that requested php

In Joomla auto updater the file is requested from one domain
http://download.abc.com/?ext=addmenu&src=core&pro=1&file=update.zip
which then downloads a different file
/mnt/storage/vhosts/newdomain.com/httpdocs/tmp/addmenu-v1.1.4.zip
I've been trying to mimic this effect by calling a file release.php which is
header("Location: /addmenu/updates/com_addmenu.zip");
but it just downloads release.php and not com_addmenu.zip
I've also tried it with
header('HTTP/1.1 307 Temporary Redirect');
before the header(Location
But I can't get it to work this way. I'm guessing that I'm not able to substitute one file for another but I'm hoping someone can help.
thanks
I understood that you have a zip file on your server which shall be downloaded under a different name by the visitor's browser when s/he opens up your webpage.php.
<?php
$file_url = '/addmenu/updates/release.zip';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename=com_addmenu.zip');
readfile($file_url);

Download existing file from server to client using php

I'm trying to make a distant client able to download an existing file(csv) named "source.csv" in my local machine using php.
After downloading the file the client should have a file named sourceCopy.csvwith the same content as the source file.
Here's what I've tried :
$file_url = 'source.csv';
header('Content-Type: application/csv');
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename="sourceCopy.csv"');
readfile($file_url);
The file is downloaded but it doesn't match the source file !

PDF file not correctly encoded when downloading a pdf file from php

This might be a duplicate but I have searched for the solution for so long and I still could not solve my problem. I have a localhost server where it stored some pdf files for download.
I implemented the following function to force the download:
if(isset($_POST['dlPDF']))
{
$file = $_SERVER['DOCUMENT_ROOT'] .'/Upload/'.$pdfName;
header("Content-Type: application/pdf");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$pdfName");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($file);
}
However I could not open the pdf file properly since it says "the pdf file is not correctly encoded".
FYI, the downloaded pdf file is always a few Kb larger than the original file.
Please kindly let me know if there is anything I can do to make the pdf readable and I appreciate your help.

Force download on a ftp:// link

I have a PHP that scans the files in a remote NAS Hard Disk via FTP protocol, generates a json file and then via javascript I list those files in the browser.
When the user click a link to a mp4, jpg and many browser-known formats, the browser opens te content instead of downloading it.
Now, I know that with PHP or .htaccess I can change the headers to force the browser to download the file but the file is in a remote location and can only be access via FTP so I can't run PHP or .htaccess in it.
I tried this header variations in PHP:
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
or
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/image");
header("Content-Transfer-Encoding: binary");
all ending with:
header("Location: $url");
(url being ftp://user:password#dyndns.org/folder/file.mp4)
but it always opens the file in the browser instead of downloading (on recognized file extensions of course)
Any ideas? Thanks
The previous headers may not work when using a redirect. Instead, you better serve them via PHP instead of redirecting:
header("Content-type: octet/stream");
header("Content-disposition: attachment; filename=".$file);
header("Content-Length: ".filesize($file));
readfile($file);
But note, that this WILL use your own bandwidth. However, there isn't any way to force a behavior on a remote host.

Categories