i have a script that generates a text file, and stores in a specific directory with a unique name, each day on my server. I’m trying to write a script to download whatever files are present in that directory. All my attempts download the first file only. I need to get control back to the foreach loop in my main script. To download, I’ve tried using a function within my script, using an external PHP script, using javascript location, and even the image src trick. My download code is just setting Header prams and using readfile
ok, not sure why the rest of my message was cut off…
I know help is usually not provided without user code, although I’m looking for theory rather than code, but will gladly accept any functional code, just the same.
[php]
$files = scandir($dir[$id]);
foreach ($files as $file) {
if ($file != “.” && $file != “…”) {
header(“Location: dlbaby.php?d=”.$dir[$id]."&f=".$file." "); // tried multiple ways!
}
}
// and dlbaby.php code //
$dir=$_GET[‘d’];
$name=$_GET[‘f’];
header(“Content-Type: text/force-download”);
header(“Content-Disposition: attachment; filename=”$name"");
readfile($dir.$name);
Well, first, you can not download more than one file using headers.
Once you send a header to the browser, the page is completed. I just spent the last couple hours reading about file transfers using PHP. It seems you may be able to do it thru a FTP command, but, you have to have FTP set up on your local system. Otherwise, you will have to do it with CLIENT-SIDE code. PHP is SERVER-SIDE only and can not send anything out except to the browser. The browser accepts this post and “RENDERS” it to the screen. If the browser finds download headers, it processes them which starts the ONE download.
Seems there might be a couple other ways to do this. You could create a a download.php page that keeps files in a session variable and loops thru them one at a time. You would be required to press a download button for each file. (A pain!)
So, after a couple more hours of studying, I found you should be able to do it with this code. The first part reads a web file in binary mode into a variable. Then, the second part writes the file out locally in binary mode. This basically does the same as a download. Note that it appears that you may have problems if the file is very large. Sort the results matters on the inputs. (But, another way is to create a SERVER-SIDE zip file and download only that one file. ) So, try to put this into your code…
[php]
$files = scandir($dir[$id]);
foreach ($files as $file) {
if ($file != “.” && $file != “…”) {
// First load the input file into a variable…
$handle = fopen(“http://www.youdomain.com/yourfolder/” . $file, “rb”);
$filecontents = ‘’;
while (!feof($handle)) {
$filecontents = $filecontents . fread($handle, 8192);
}
fclose($handle);
// Now write the file locally to your folder...
$localfolder = "C:\username\somefoldername\";
$handle = fopen($localfolder . $file, 'wb');
fwrite($handle, $filecontents);
fclose($handle);
}
}
[/php]
So, I did not have time to test these routines, and you have to change the pointers for the files to your correct ones. The “rb” and “wb” mean read-binary and write-binary. It seems that the fopen function will open local or remote files. Once the file is opened, you can use it however you wish. Let us know if you get it working!