I am building a site that allows the user to download a set of images based on user selection. How can I have downloads of multiple files? is it possible to download more than 1 file at a time?
If it is only possible to download 1 file at a time, how can I "create" 5 files, get them zipped, and let the user download that product zip file?
Thanks
You can only do only one download at a time.
If you like to zip the files have a look at this.
You can create the files using file_put_contents().
Update
On second thought... Maybe you are able to start multiple downloads if you open a new tab/window for each file. But the browser might block those popups:
<script type="text/javascript">
window.open('download.php?name=file1','_newtab');
window.open('download.php?name=file2','_newtab');
window.open('download.php?name=file3','_newtab');
</script>
You can't download multiple files. You have to zip them and redirect the user's browers on the zip.
You could use the PHP ZIP class to suit your needs.
This code creates a zip archive for you:
<?php
function createZipFile($files = array(), $filePath){
// Check if $files we had contains files
if(is_array($files)){
// $files contains files, loop through them
foreach($files as $file){
// Does the file exist?
if(file_exists($file)){
$filesToBeZipped[] = $file;
}
}
}
if(count($filesToBeZipped)){
// Create your new zip archive
$zipArchive = new ZipArchive();
if($zipArchive->open($filePath, ZIPARCHIVE::CREATE) !== true){
return false;
}else{
foreach($filesToBeZipped as $file) {
$zipArchive->addFile($file,$file);
}
$zipArchive->close();
return file_exists($filePath);
}
}else{
return false;
}
}
$files = array(
'filename1.extension',
'/path/to/filename/filename.extension',
);
$action = createZipFile($files,'yourSampleArchive.zip');
?>
You can download a lof of zipped file formats, look at this:
http://www.php.net/manual/en/refs.compression.php
Php can only send one file at a time, but you can generate javascript code with php which calls multiple php files.
Related
I have written the PHP script to generate the zip file. it's working fine when I use rar software to extract it but not getting extract with rar software. I can't ask to users to install rar software to extract downloaded zip file.
I don't know where i am commiting mistakes.
Here i attached error screen shot which i get when try to open zip file.
// Here is code snippet
$obj->create_zip($files_to_zip, $dir . '/download.zip');
// Code for create_zip function
//create the archive
$zip = new ZipArchive();
if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach ($valid_files as $file) {
$filearr = explode('/', $file);
$zip->addFile($file, end($filearr));
}
$zip->close();
If $valid_files is a glob'd array, use basename() instead of end(), your zip might not actually have added any files causing for it to be an invalid zip (however that would be visible in the size of the zip file).
Also try winrar/winzip/7zip and see what they return, microsoft's internal zip engine might not be up to date enough to open the zips.
I have also encountered this problem, using 7z solved the problem but we need to send the zip to somebody else so 7z is a nono.
I found that, in my case it is that the file path is too long:
When I use this:
$zip->addFile($files_path.'/people.txt');
And it generated a zip folder nested very deep e.g. ["/tmp/something/something1/something2/people.txt"]
So I need to use this instead
$zip->addFile($files_path.'/people.txt', 'people.txt');
Which generate a a zip folder with only 1 layer ["people.txt"], and Windows Zip read perfectly~
Hope this helps somebody that also have this problem!
I have a situation, I have list of user data, in that data details, i have stored a file also.
Now i want to download all the file of users in zip file, when a user click on a button like DOWNLOAD.
i have written a action in controller where im fetching the file to be downloaded, but im unable to write code for making zip finle and download it.
Below is my code in controller.
public function actionMain($jid)
{
$values = UserRequest::find()->where(['sender_code'=>$jid,'status'=>1])->all();
$i=1; foreach($values as $data) {
$users = Users::find()->where(['access_code'=>$data->reciever_code])->one();
$file_names[]= $users->attach_cv;
}
}
This is working fine. in $file_name im fetching all the file's, But i want to download this files in a zip foder.
Please help me to solve this issue
The link provided is in core php and easily implemented in that. But how about doing the same in framework. Because i have array of file name in action. As you can see above. DO i need to write the code for generating .ZIP in the same action or any other action.
I would recommend using a ZipArchive as specified in the comment by SiZE.
That way you initiate the zipping process before you start the download.
File creation:
/**
* Generate zip file for upload
*
* #throws Exception
*/
private function createZip($files)
{
$file = 'full_path_to_file/your_file_name.zip';
$zip = new ZipArchive();
if ($zip->open($file, ZipArchive::CREATE) !== TRUE) {
throw new \Exception('Cannot create a zip file');
}
foreach($files as $file){
$zip->addFile($file[file_name], $file[local_name]);
}
$zip->close();
}
And then you can return the zip file path for download in your action
I have an .srt file located in the files/srt/username/filename.srt directory. I need to be able to download it in the browser, but to make this possible I have to zip the file first.
I found the following code online:
function download_zip() {
if (isset($_POST['download_srt'])) {
$zip = new ZipArchive();
$zip->open("files/srt/".$_SESSION['user_name']."/".$_POST['download_srt'].".zip", ZipArchive::CREATE);
// output: files/srt/username/filename.srt.zip
$zip->addFile($_POST['download_srt']);
// output of $_POST['download_srt']: filename.srt
$zip->close();
}
}
The code is called when a submit button is pressed and the $_POST data are sent.
The function works, but no ZIP file gets created in the same directory as the original srt file. No error messages appear.
You should provide the correct path to the file to add:
$zip->addFile("files/srt/".$_SESSION['user_name']."/".$_POST['download_srt']);
Is it possible to open a ZIP file on a server, read a file from its content and display it / send it to a client directly without extracting it to disk first ? I am talking about pdf's and images. Haven't found any hints in the php sites.
Well,there is a PHP Extension.
If you use the extractTo method, you would be able to extract a single file, checkout the documentation.
From the documentation, extracting two files:-
<?php
$zip = new ZipArchive;
$res = $zip->open('test_im.zip');
if ($res === TRUE) {
$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
You would need to provide an array of path inside the zip.
I'm creating Yii2 application that allows users to download tutorial and it's subtitle. Currently I can do it separately but, what I want to do is to create a new folder with the video name and include both video and subtitle file in that folder and then create a zip and give that to the user for download. but I have no idea how to do it. I can zip the files using php ZipArchive but i have no idea how to create new folder and include those two files in to it.
my download action is
public function actionDownload($id)
{
// get all videos relevan for this video id
$video = TempVideo::findOne($id);
$file = $video->path;
if (file_exists($file))
{
Yii::$app->response->sendFile($file);
}
}
helps are highly appreciated.
You could use ZipArchive
http://php.net/manual/en/zip.examples.php
or launch command line with:
system('zip filecompress.zip file1 file2 file3');
I don't have all the information but i think it need to mlook something like this:
you need to make a new directory with mkdir() (doc: http://php.net/manual/en/function.mkdir.php);
and copy the needed files with copy()
(doc:http://php.net/manual/en/function.copy.php)
public function actionDownload($id)
{
// get all videos relevan for this video id
$video = TempVideo::findOne($id);
$file = $video->path;
if (file_exists($file))
{
Yii::$app->response->sendFile($file);
$new_video_path = '/path/to/save/dir';
//create a new dir
mkdir($new_video_path);
//copy the file to a new path
copy($file,$new_video_path.'newfilename.mp4');
copy('subtitle/pathe.txt',$new_video_path.'subtitle.txt');
}
}
and after that you can create a zip file with the ZipArchive that doc can be found here: http://php.net/manual/en/class.ziparchive.php