Jump to content

Help with download


Lassie

Recommended Posts

I am trying to finish a download operation and in difficulty.
My script is attempting to retieve download locations from a temporary hash directory that contains the product files.
I am having difficulty creating
1.File path for the download
2.Getting the file to open and display a (browser?) alert that would prompt the user to download or save the file.
3.My code also needs to account for the fact that the folder may contain multiple files to download.

My code is
[code]
$nogo = 'Sorry, download unavailable. <a href="index.php">Back</a>.';
define('DOWNLOAD_DIR', 'C:/pick_up/');

//open the dir
$folder= opendir("C:pick_up/$hash");

// prepare the download
//initialise an array to store the contents
$files = array();
//loop through the directory
while(false !==($item=readdir($folder)))
{
$files[] = $item;
}
closedir($folder);
if ($files)
{
foreach ($files as $filename){
echo "The value of $files is $filename";//debug
}
}

$filepath = 'C:/pick_up/$hash/'.$filename;
// check that it exists and is readable
if (file_exists($filepath) && is_readable($filepath))
{

// get the file's size and send the appropriate headers

$size = filesize($filepath);
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode

$file = fopen($filepath, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);

exit;
}
else {
echo "$nogo";
  }
}
[/code]

I have printed the files array which sows as follows;
18da371787b907b8f2506d1e89789f9chash match madeC:/pick_up/18da371787b907b8f2506d1e89789f9cThe value of Array is .The value of Array is ..The value of Array is How to Buy a Car With Little or no credit.pdf

Array
(
    [0] => .
    [1] => ..
    [2] => How to Buy a Car With Little or no credit.pdf
)
Any guidance on this is much appreciated.
Lassie
Link to comment
https://forums.phpfreaks.com/topic/34593-help-with-download/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.