Bottyz Posted March 2, 2010 Share Posted March 2, 2010 Hi All, I'm new to the file downloading side of things and am a bit lost with a couple of problems i've come across. I want to be able to download, zip, jpg, pdf and a few other file types but those are the most important. I've tried a few tutorials i've found and combined them with some of my own script. I've tested downloading a zip file and it appears as a download but doesn't appear to download properly as it says its currupted and downloads too quickly for a 32mb file! code is as below: if ($access < 1) { //if user access not granted to file category return message if($logging > 0){ $status = "WrongPermissions"; include('logit.php'); } $errors = "ERROR: You don't have permission to access that file!"; $_SESSION['$fileERROR'] = $errors; $redirect = $_SESSION['PrevUrl']; header("Location: ". $redirect ); exit; } //if file exists and user access granted: // define the path to your download folder plus assign the file name $path = 'files/'.$filename; // check that file exists and is readable if (file_exists($path) && is_readable($path)) { // get the file size and send the http headers $size = filesize($path); 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 // display the error messages if the file can´t be opened $file = @ fopen($path, 'rb'); if ($file) { if($logging == 1){ $status = "Granted"; include('logit.php'); } // stream the file and exit the script when complete fpassthru($file); exit; } else { if($logging == 1){ $status = "CantOpen"; include('logit.php'); } $errors = "ERROR: Can't open requested file!"; $_SESSION['$fileERROR'] = $errors; $redirect = $_SESSION['PrevUrl']; header("Location: ". $redirect ); exit; } } Is it something to do with the content headers? Another problem is that once the file has been offered up for download, it doesn't log the download in my mysql table. i've echoed out the $logging variable and thats ok. It seems to miss the logging part all together, as i tried echoing out a hello world followed by an exit but it doesn't get that far. Any help would as always, be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/193874-help-with-file-downloads/ Share on other sites More sharing options...
Bottyz Posted March 2, 2010 Author Share Posted March 2, 2010 the more i look into this the harder it seems to be. I think it might have something to do with my content-length header as the $size variable echos correctly but when i goto download and check the content-length header value, its completely different. Anybody got some code they know works for downloading files? Quote Link to comment https://forums.phpfreaks.com/topic/193874-help-with-file-downloads/#findComment-1020341 Share on other sites More sharing options...
Bottyz Posted March 2, 2010 Author Share Posted March 2, 2010 ok, so probing a bit more into this all files no matter what size they are... are downloading as 1kb only. Any ideas? fileszie and filename are correct, as i've tried echoing and downloading directly outside the code. my code now looks like this: // if file exists and user access granted: // define the path to your download folder plus assign the file name $path = 'files/'.$filename; // check that file exists and is readable if (file_exists($path) && is_readable($path)) { // get the file size and send the http headers $size = filesize($path); // fix for IE catching or PHP bug issue header("Pragma: public"); // set expiration time header("Expires: 0"); // browser must download file from server instead of cache header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // force download dialog header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); // set file size header("Content-Length: " .$size); // set file name header('Content-Disposition: attachment; filename="'.$filename.'"'); //set encoding header('Content-Transfer-Encoding: binary'); // open the file in binary read-only mode // display the error messages if the file can´t be opened $file = @ fopen($path, 'rb'); if ($file) { if($logging == 1){ $status = "Granted"; include('logit.php'); } // stream the file and exit the script when complete fpassthru($file); exit; } else { if($logging == 1){ $status = "CantOpen"; include('logit.php'); } $errors = "ERROR: Can't open requested file!"; $_SESSION['$fileERROR'] = $errors; $redirect = $_SESSION['PrevUrl']; header("Location: ". $redirect ); exit; } } i'm not sure the code is reaching the fpassthru() line but it does log an access granted entry to the log mysql. Quote Link to comment https://forums.phpfreaks.com/topic/193874-help-with-file-downloads/#findComment-1020346 Share on other sites More sharing options...
harristweed Posted March 2, 2010 Share Posted March 2, 2010 why not just link to the file? or is that too easy? Quote Link to comment https://forums.phpfreaks.com/topic/193874-help-with-file-downloads/#findComment-1020355 Share on other sites More sharing options...
Bottyz Posted March 2, 2010 Author Share Posted March 2, 2010 why not just link to the file? or is that too easy? I want to make it so that no one unauthorised can download the files. So it needs to transparently present the files without the user being able to see the file paths. Quote Link to comment https://forums.phpfreaks.com/topic/193874-help-with-file-downloads/#findComment-1020394 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.