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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.