Downloading multiple files using php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP based multiple files download from URL class

qaribhaider/php-multifile-download

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

PHP based multiple files download from URL class

Use this class to download multiple files from remote URLs to a directory, in a single request. The class uses cURL, so make sure it is installed

$files_to_download = array( "http://lorempixel.com/output/nightlife-q-g-640-480-4.jpg", "http://lorempixel.com/output/abstract-q-c-640-222-1.jpg", "http://lorempixel.com/output/sports-h-g-640-800-7.jpg" );
$md = new MultiDownload(); $md->setSavePath("./downloads/"); $md->setURLs($files_to_download); $md->download();

About

PHP based multiple files download from URL class

Источник

php header to download multiple files in loop

Short answer is no, you need something to tell the client where the content ends and a new header begins. You can either use the zip utility and package them up for download, or you could look in to maybe a mime email format (which uses dividers throughout the document) but I’m not sure that would work over the HTTP protocol without explicitly calling it an «email» package.

I would just recommend using the zip utility. Generate the PDFs you need to, then package them up and send them off.

No. HTTP doesn’t support multiple files in a single download. There was some talk about adding MIME-style semantics to HTTP messages way back when, so you could embed multiple responses in a single transfer, but that didn’t go anywhere.

Unless you do something like zipping the multiple files on the server and transfer that zip, there’s no way to download more than one file per request.

Step1: Create one index.php file and add following code.

Step2: Create one download.php file and add following code.

 "application/pdf", "txt" => "text/plain", "html" => "text/html", "htm" => "text/html", "exe" => "application/octet-stream", "zip" => "application/zip", "doc" => "application/msword", "xls" => "application/vnd.ms-excel", "ppt" => "application/vnd.ms-powerpoint", "gif" => "image/gif", "png" => "image/png", "jpeg"=> "image/jpg", "jpg" => "image/jpg", "php" => "text/plain" ); if($mime_type=='') < $file_extension = strtolower(substr(strrchr($file,"."),1)); if(array_key_exists($file_extension, $known_mime_types))< $mime_type=$known_mime_types[$file_extension]; >else < $mime_type="application/force-download"; >; >; //turn off output buffering to decrease cpu usage @ob_end_clean(); // required for IE, otherwise Content-Disposition may be ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header('Content-Type: ' . $mime_type); header('Content-Disposition: attachment; filename="'.$name.'"'); header("Content-Transfer-Encoding: binary"); header('Accept-Ranges: bytes'); /* The three lines below basically make the download non-cacheable */ header("Cache-control: private"); header('Pragma: private'); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // multipart-download and download resuming support if(isset($_SERVER['HTTP_RANGE'])) < list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2); list($range) = explode(",",$range,2); list($range, $range_end) = explode("-", $range); $range=intval($range); if(!$range_end) < $range_end=$size-1; >else < $range_end=intval($range_end); >$new_length = $range_end-$range+1; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range-$range_end/$size"); > else < $new_length=$size; header("Content-Length: ".$size); >/* Will output the file itself */ $chunksize = 1*(1024*1024); //you may want to change this $bytes_send = 0; if ($file = fopen($file, 'r')) < if(isset($_SERVER['HTTP_RANGE'])) fseek($file, $range); while(!feof($file) && (!connection_aborted()) && ($bytes_send<$new_length) ) < $buffer = fread($file, $chunksize); print($buffer); //echo($buffer); // can also possible flush(); $bytes_send += strlen($buffer); >fclose($file); > else //If no permissiion die('Error - can not open file.'); //die die(); > //Set the time out set_time_limit(0); //path to the file $file_path=$_SERVER['DOCUMENT_ROOT'].'/your_folder/'.$_REQUEST['filename']; //Call the download function with file path,file name and file type output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain'); ?> 

Change ‘your_folder’ with you folder name in both files.

Источник

How Can I Download Multiple Files in a Directory?

I have a list of filenames that exist in a directory online. What is the best way to download them all? For example I want to get the following files:

516d0f278f14d6a2fd2d99d326bed18b.jpg b09de91688d13a1c45dda8756dadc8e6.jpg 366f737007417ea3aaafc5826aefe490.jpg 
$var = filelist.txt for ( $i in $var ) < wget http://media.shopatron.com/media/mfg/10079/product_image/$i >

Possible duplicate: stackoverflow.com/questions/15436388/… The answer there seems to apply here, too.

3 Answers 3

$list = file_get_contents('path/to/filelist.txt'); $files = explode("\n", $list); ## Explode around new-line. foreach ($files as $file) < file_put_contents('new_filename.jpg', file_get_contents('http://url/to/file/' . $file)); >

Basically you explode the list around the new-line to get each row, and then file_put_contents the file right after the server downloads it from wherever you are getting them from.

file_get_contents() might not work on some (or «many») hosts, see: stackoverflow.com/questions/7794604/…

$files = file('filelist.txt'); //this will load all lines in the file into an array $dest = '/tmp/'; //your destination dir $url_base = 'http://media.shopatron.com/media/mfg/10079/product_image/'; foreach($files as $f)

Pretty self-explanatory, but one point: if you’re unsure of filelist.txt’s contents, you should clean the filenames.

In response to Pat’s question below.. Both version are IO bound so speed won’t be very different. However file_get_contents is superior to your wget solution for several reasons: 1) Running exec introduces big security risks and is disabled on some systems, 2) some servers don’t have wget installed.

I see, but according to stackoverflow.com/a/7794628/2097294 using file_get_contents also may not be enabled on some servers, right?

You’re right. Security risk is the bigger issue. And while there are risks with both fopen and exec, one generally wants to minimize use of the commands that allows access to run arbitrary commands on your OS (exec, system, passthru). For that reason, exec is usually a last resort for php coders.

1. The way this code works, the filelist.txt needs to have each filename be a separate line in the file. 2. filelist.txt file needs to be in the same folder as the script, 3, the filelist.txt names need to have no extra white-space, and 4. To get help, it would help if you shared the error you got

Источник

Download Multiple files in one HTTP request

how is it possible to download multiple files in one HTTP request? what i mean it’s like when you have multiple attachments and you select what you want to download then press download so they will be automaticcaly downloaded and you don’t have to click on each one manually. i’m using PHP as a serverside srcipting.

8 Answers 8

In general, HTTP treats a multipart message-body no differently than any other media type: strictly as payload. […] an HTTP user agent SHOULD follow the same or similar behavior as a MIME user agent would upon receipt of a multipart type.

[…] If an application receives an unrecognized multipart subtype, the application MUST treat it as being equivalent to «multipart/mixed».

But since Firefox is the only browser that I know about to support such multipart responses (apart from multipart/byterange), you should use some archive file format for this purpose.

That is practically not usable due to poor browser support. You can pack them into a tar or zip file at server side and serve the archive file though.

It is practically possible (Firefox proves it). But it is practically not useful since it’s only Firefox that supports it.

More clarification then 🙂 BTW, since browser developers are leaning against more public stuff like html5 elements and CSS3, I doubt that we will see a widespread adoption of this anytime soon. Good to see that FF implemented it though.

I think it’s not possible since each HTTP request has only one URI.

Correct. HTTP requests have a specific start and end. You could probably abuse the protocol by writing a special client and serving the files as one file (much like a tar file or just a MIME multipart ), but then we wouldn’t be talking about a pure HTTP solution that works across all browsers anymore.

You can zip the file with PHP, serverside, and request the file or return it from within your script by setting the appropriate headers, see ZipArchive class

Or you create a special client that can parse your then self-specified message format (a flash app, a plugin) — but if your client is simply your browser you’ll get one response with a fixed content-length from the server.

Dunno about one HTTP request, but if you want them all at once you can loop through them while changing the header(‘Location:’) for each of them while redirecting to the immediate download script. Though, that would be redundant, and ugly; I think the best way would be to zip them all, there are instructions on how to do so in the PHP Documentation.

It’s not possible in that way, because if you’ll send location header download will not start and browser will be redirected, otherwise if file will be downloaded no any redirect will be done.

I’m using multiple invisible iframes to trigger multiple downloads at once. There will be more than one HTTP request, but the effect will be what you described.

Most browsers will ask the user if they want to allow multiple downloads (this is to prevent letting website spamming the file system). Also, you have to make sure the file will actually be downloaded, and not just displayed inside the invisible iframe (e.g. by using header(‘Content-disposition: attachment’); inside the PHP script serving the file).

In my solution I use JavaScript to change the src after each file is downloaded, so the files load sequentially and my server has to handle fewer requests at once. But that solution requires AJAX and is much more complicated.

Источник

Читайте также:  Linked list python code
Оцените статью