The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php download multiple files problem. Reply To: php download multiple files problem.



I’m trying to work on this now and am having trouble.
I’ll probably post some of my JS and php to see if you spot anything I’m doing wrong :)

I’m not sure why it would be “improper.” If you’re not zipping the files, this is the only way to get more than one downloaded on a single user action.

Oh, it seems very inefficient. Let’s say I wanted to download 20 images. To have JS loop through 20 times contacting the php script seems a lot more work to the server and client than just contacting the server once with a list of the 20 images.

I guess that’s how HTTP works though, I’m still new so learning.

OK, gonna post some not working code. Maybe you can spot what I’m doing wrong?

My current JS code function which passes the picture name to the php script which is supposed to do the download:

function downloadImages()
{ // selected_Images is a global array that contains all of the image names that were clicked on.
var xmlhttpdl;

if (window.XMLHttpRequest)
  { // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttpdl=new XMLHttpRequest();
  { // code for IE6, IE5
  xmlhttpdl=new ActiveXObject("Microsoft.XMLHTTP");
  if (xmlhttpdl.readyState==4 && xmlhttpdl.status==200)
  // ---------------- May use this commented code later to display results on the page. 
  // var temp_responsedownload=xmlhttpdl.responseText; 
  // alert ("php script sent us :"+temp_responsedownload);  
  // for (var mycounterdownload=0; mycounterdownload<maxicountdownload; ++mycounterdownload)
  // { document.getElementById("demo").innerHTML=xmlhttpdl.responseText;  }

// Here we try to loop and send each image filename in it's own call to the server.
for (var itemCountDownload=0; itemCountDownload <= selected_Images.length; itemCountDownload++)
{"POST","http://www.theReceivingPhpScriptHere.php",true); // false = wait for reply.
 // not using the looped variable yet. selected_Images[itemCountDownload].
// for testing I'm just passing a real image name to the php script to see if even 1 file
// downloads.

} // end function

I think I just found one JS error. I think I’m supposed only loop the send call and not the other parts like this:"POST","http://www.theReceivingPhpScriptHere.php",true); // false = wait for reply.
for (var itemCountDownload=0; itemCountDownload <= selected_Images.length; itemCountDownload++)

Since in my test I’m only trying to send one file that doesn’t explain why the one file isn’t downloading.
I must have bugs on the php script now too.

And the current state of the receiving php script is:

// You MUST REPLACE thre things in this script:
// 1. The $error path so it redirects to the page you want.
// 2. You must change $filepath to be the internal server path to the directory storing your images.
// 3. You must replace the image names to correct image names.

$error=''; // redirect to a page you want.

// YOU MUST replace the $filepath with your server's internal path to the directory holding the images.
$filepath='/path/to/images/'; // leave the / on the end because we'll be adding the filename to it to form the complete path.

$getfile = isset($_POST['downloadfile']) ? $_POST['downloadfile'] : '.fubar.'; 
// Run basename on the getfile after testing works.

//if ($getfile) 
  $path = $filepath . $getfile;
  // check that it exists and is readable
  if (file_exists($path) && is_readable($path)) 
    // get the file's size and send the appropriate headers
    $size = filesize($path);
    header('Content-Type: application/octet-stream');
    header('Content-Length: '. $size);
    header('Content-Disposition: attachment; filename=' . $getfile);
    header('Content-Transfer-Encoding: binary');
    // open the file in read-only mode
    // suppress error messages if the file can't be opened
    $file = @ fopen($path, 'r');
    if ($file) 
    { fpassthru($file); exit;} 
    { header("Location: $error");}
        { header("Location: $error");}

Am really tired now. Apologies if I’ve missed something easy and am wasting your time.