Jump to content

Download serve and detect success/fail


walter1917

Recommended Posts

Hi all,

 

I am trying to serve a small text file to my users with a download. I need a flag to use to detect whether or not the file was downloaded fully or crashed/aborted before. Here's the code I've found from working on other sites.

 

Currently, it allows for download. But I see neither the first echo 'Welcome' nor any of the other echos (Success/Fail). What is going on here?

 

 

Thanks!

 

 


function sendTest()
{
$res = null;
$res = sendFile('testimage.gif', 'image/gif');

if ($res['status']) {
// Download succeeded
echo "Success!";
} 

else {
// Download failed
echo "Fail!";
}

return;


}

function sendFile($path, $contentType = 'image/gif')
{
ignore_user_abort(true);

header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' .basename($path). "\";");
header("Content-Type: $contentType");
header('Expires: 0');
header('Cache-Control: public, must-revalidate, max-age=0'); // 1 hour

$res = array('status' =>true, 'errors' =>array(), 'readfileStatus' =>null, 'aborted' =>false);

ob_clean();
flush();
$res['readfileStatus'] = readfile($path);

if ($res['readfileStatus'] == false) 
{
$res['errors'][] = 'readfile failed.';
$res['status'] = false;
}

if (connection_aborted()) 
{
$res['errors'][] = 'Connection aborted.';
$res['aborted'] = true;
$res['status'] = false;
}

return $res;
}

echo "Welcome";
$res = sendTest();
echo "Done";





 

 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/203916-download-serve-and-detect-successfail/
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.