I am looking to create a PHP script which will create a zip file from a location which will be a variable passed via $_GET. The location will be a folder which will then be zipped up and the user will be prompted to download the folder, after download the folder will need to automatically be deleted.
Can anyone help?
Thanks
http://php.net/manual/en/ref.zip.php
You could do something like this:
validate get
look up if the folder exists
zip the folder
read the newly created zip file and delete it at the end, like this:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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));
ob_clean();
flush();
readfile($file);
#unlink($file);
Code taken frome here: http://php.net/manual/en/function.readfile.php
read the location from $_GET and read all files under and create an array of files. than create a zip and download.
following link will help you
http://www.tricksofit.com/2013/10/create-zip-file-php
Related
I aim to download the news.json file in a PHP website from my computer file directory. When click on a button, it calls the downloadFile.php to force download the file from my computer. The codes work for downloading other files like images and docs except json and csv files. Is there any way to download the mentioned file types or my code has some problem.
$file = ("C:\Users\THINKPAD\Downloads\my_file_name.png");
$filetype=filetype($file);
$filename=basename($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));
ob_clean();
flush();
readfile($file);
I have tried to download file from server using below code but it not downloaded. It shows only content in browser
<?php
if(file_exists($zipName))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=\'Uttam_Solanki.zip\'');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
exit;
}
?>
you need to change role in .htaccess
add application/octet-stream zip
your content-type must be `application/zip
here is a similar approach:
send zip file to browser / force direct download
How are you triggering this script?
If by link, maybe using download attribute would help?
Is there any way to provide a direct link to a file and force the browser to download it using PHP?
E.g http://www.website.com/directory/file.jpg
We're dealing with huge files here and Chrome in particular seems to have a problem rendering the image, so all the user sees is a blank screen when visiting the file directly. Even though they can still right-click in the blank screen and download the file it's confusing.
We used to output the files from PHP but we ran into memory problems so switched to providing a direct link instead. The files go up to about 5GB, they aren't all images. We have zips, PDFs, PSDs etc.
Currently, the file is requested through a PHP script which accepts the ID of the file and get its URL. The PHP script then redirects to the user to full URL of the file.
How can we ensure that downloads are forced and we don't run into memory issues with the larger files?
Thank you
Just use X-Sendfile but you need to configure it first ... using XSendFilePath
if (file_exists($file)) {
header("X-Sendfile: $file");
header("Content-Type: application/octet-stream");
header(sprintf("Content-Disposition: attachment; filename=\"%s\"", basename($file)));
exit();
}
Note* Please ensure $file is properly escaped before you verify and serve the file
XSendFilePath only works on Apache for other servers please see : Caching HTTP responses when they are dynamically created by PHP
You need to set the headers for force download
$file = 'upload_directory_path/'.$image_name;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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));
ob_clean();
flush();
readfile($file);
exit;
}
<?php
//file path
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
I have this code in download.php file:
$file = //path to file (for example .xlsx file)
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=file.xlsx');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
So, if open site.com/download.php, excel file is downloaded.
My question is:
somehow, downloader can understand what is a downloaded xlsx file location on the server? or this is impossible?
No. The client will have no idea where on the server the excel file came from.
You could be generating it in realtime or scraping it from another site or loading from a file. No way to tell.
I am using WordPress for a web site, and would like to include a menu item to download a zip file from the web site to the local drive. I tried using the following function:
function download_binary_file($file) {
if (file_exists($file)) {
$base_name = basename($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$base_name.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
But when this function is executed, instead of getting a dialog to save the file, the contents of the zip file are displayed in my browser. Any ideas?
Upload your zip file, then create the menu item, then add a htaccess redirect from the menu item to the zip file url. Zip files usually auto download.. I know there are also download manager plugins for wordpress, that make the process pretty easy.