sowmithrii Posted November 30, 2007 Share Posted November 30, 2007 Am presently debugging the code snipped which someone has written, and am pretty new to php. just 15 days old. I am facing few problems in the code.... mainly in the download section. i have gone through all the code realated to that section... but i dint find any problem The actuall problem am facing is... the customers are supposed to download our companies software.. and the logic is working fine.. but suddenly the download page was getting blank this download page contains the code which redirects to a function x, and that function sends the file to the user. in that function x... the previous coder has used @readfile($file_path) to send the download... is this occassional error is just becos of readfile function or some other... please help me guys... Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/ Share on other sites More sharing options...
MadTechie Posted November 30, 2007 Share Posted November 30, 2007 can you post some code. it should look something like this Force Downloads Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402782 Share on other sites More sharing options...
sowmithrii Posted November 30, 2007 Author Share Posted November 30, 2007 This is all the code... pls get me a solution/////////////////////////////////////////////////////////// function sendDownLoadFile($base_directory, $file_name) { $allowed_ext = array ( 'txt' => 'text/text', // archives 'zip' => 'application/zip', // documents 'pdf' => 'application/pdf', 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', // executables 'exe' => 'application/octet-stream', // images 'gif' => 'image/gif', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', // audio 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav', // video 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo' ); #################################################################### ### DO NOT CHANGE BELOW #################################################################### // Make sure program execution doesn't time out // Set maximum script execution time in seconds (0 means no limit) set_time_limit(0); // Get real file name. // Remove any path info to avoid hacking by adding relative path, etc. $fname = basename($file_name); // Check if the file exists // Check in subfolders too function find_file ($dirname, $fname, &$file_path) { $dir = opendir($dirname); while ($file = readdir($dir)) { if (empty($file_path) && $file != '.' && $file != '..') { if (is_dir($dirname.'/'.$file)) { find_file($dirname.'/'.$file, $fname, $file_path); } else { if (file_exists($dirname.'/'.$fname)) { $file_path = $dirname.'/'.$fname; return; } } } } } // find_file // get full file path (including subfolders) $file_path = ''; find_file($base_directory, $fname, $file_path); if (!is_file($file_path)) { return false; } // file size in bytes $fsize = filesize($file_path); // file extension $fext = strtolower(substr(strrchr($fname,"."),1)); // check if allowed extension if (!array_key_exists($fext, $allowed_ext)) { return false; } // get mime type if ($allowed_ext[$fext] == '') { $mtype = ''; // mime type is not set, get from server settings if (function_exists('mime_content_type')) { $mtype = mime_content_type($file_path); } else if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME); // return mime type $mtype = finfo_file($finfo, $file_path); finfo_close($finfo); } if ($mtype == '') { $mtype = "application/force-download"; } } else { // get mime type defined by admin $mtype = $allowed_ext[$fext]; } // Browser will try to save file with this filename, regardless original filename. // You can override it if needed. $asfname = $fname; // set headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $mtype"); header("Content-Disposition: attachment; filename=\"$asfname\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $fsize); // download @readfile($file_path); } Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402805 Share on other sites More sharing options...
MadTechie Posted November 30, 2007 Share Posted November 30, 2007 fixed <?php $curpath = dirname(__FILE__)."/"; sendDownLoadFile($curpath,"test.jpg"); function sendDownLoadFile($base_directory, $file_name) { $allowed_ext = array ( 'txt' => 'text/text', // archives 'zip' => 'application/zip', // documents 'pdf' => 'application/pdf', 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', // executables 'exe' => 'application/octet-stream', // images 'gif' => 'image/gif', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', // audio 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav', // video 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo' ); #################################################################### ### DO NOT CHANGE BELOW #################################################################### // Make sure program execution doesn't time out // Set maximum script execution time in seconds (0 means no limit) set_time_limit(0); // Get real file name. // Remove any path info to avoid hacking by adding relative path, etc. $fname = basename($file_name); // Check if the file exists // Check in subfolders too // get full file path (including subfolders) $file_path = ''; $file_path = find_file($base_directory, $fname, $file_path); if (!is_file($file_path)) { return false; } // file size in bytes $fsize = filesize($file_path); // file extension $fext = strtolower(substr(strrchr($fname,"."),1)); // check if allowed extension if (!array_key_exists($fext, $allowed_ext)) { return false; } // get mime type if ($allowed_ext[$fext] == '') { $mtype = ''; // mime type is not set, get from server settings if (function_exists('mime_content_type')) { $mtype = mime_content_type($file_path); } else if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME); // return mime type $mtype = finfo_file($finfo, $file_path); finfo_close($finfo); } if ($mtype == '') { $mtype = "application/force-download"; } } else { // get mime type defined by admin $mtype = $allowed_ext[$fext]; } // Browser will try to save file with this filename, regardless original filename. // You can override it if needed. $asfname = $fname; // set headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $mtype"); header("Content-Disposition: attachment; filename=\"$asfname\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $fsize); // download @readfile($file_path); } function find_file ($dirname, $fname, &$file_path) { $dir = opendir($dirname); while ($file = readdir($dir)) { if (empty($file_path) && $file != '.' && $file != '..') { if (is_dir($dirname.'/'.$file)) { find_file($dirname.'/'.$file, $fname, $file_path); } else { if (file_exists($dirname.'/'.$fname)) { $file_path = $dirname.'/'.$fname; return $file_path; } } } } return false; } // find_file ?> Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402816 Share on other sites More sharing options...
sowmithrii Posted November 30, 2007 Author Share Posted November 30, 2007 hei,, man,. whats fixed... how u fixed that.. so simply. can u explain me the error in my code. (am verymuch new to php...) [ i am calling the function senddownloadfile($base_directory, $file_name) from different form ] let me tell u the flow , assume form1.php, form2.php. i have written function senddownloadfile(......) in form2.php and in form1.php am calling that function by passing parameters. and one more thing is,, my code is executing perfectly most of the time , but occassionally i am getting the error of blank display of the form2.ph I have used smarty templates for the display purpose Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402828 Share on other sites More sharing options...
MadTechie Posted November 30, 2007 Share Posted November 30, 2007 Main problem was here <?php $file_path = ''; find_file($base_directory, $fname, $file_path); if (!is_file($file_path)) { return false; } ?> $file_path was always empty this always returning false (ending the script) changes <?php $file_path = find_file($base_directory, $fname, $file_path); if (!is_file($file_path)) { return false; } ?> also function find_file returnd nothing if the file was found or not.. so i changed to return $file_path; Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402832 Share on other sites More sharing options...
sowmithrii Posted November 30, 2007 Author Share Posted November 30, 2007 Thanks man,,.. it will take some time for me to test whether ur solution works fine for me or not.. it seems ur solution is correct.. what does these two conditions mean in my code for function find_file() $file != '.' $file != '..' thanks anyway.. Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402849 Share on other sites More sharing options...
sowmithrii Posted November 30, 2007 Author Share Posted November 30, 2007 and one more thing is,, i code which i sent u is working perfectly and its only occassionally the problem is occuring... so , if what u have told is right, means just becos of $file_path=' '; and soo.... , then i must face the problem always,, right. Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402865 Share on other sites More sharing options...
MadTechie Posted November 30, 2007 Share Posted November 30, 2007 from the code i sent <?php $curpath = dirname(__FILE__)."/"; sendDownLoadFile($curpath,"test.jpg"); ?> Now $curpath = dirname(__FILE__)."/"; is the current path (where the script is) test.jpg = the file i am looking for, the download looks for this file in the current folder and all sub folders.. if none is found it will have a blank screen to fix this change if (!is_file($file_path)) { return false; } to if (!is_file($file_path)) { die("No file found"); } also you can change if (!array_key_exists($fext, $allowed_ext)) { return false; } to if (!array_key_exists($fext, $allowed_ext)) { die("Bad FileType"); } as for the . and .. . = parent folder .. = back one folder these exist in ever folder thus we exlude them from the search Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402873 Share on other sites More sharing options...
sowmithrii Posted November 30, 2007 Author Share Posted November 30, 2007 i accept your explanation.. but my question is... why am facing the blankpage error only few times and not always...? and wow man.. u r g8 Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402876 Share on other sites More sharing options...
MadTechie Posted November 30, 2007 Share Posted November 30, 2007 if you add the changes you shouldn't get any blank screens but inplace an error telling why the blank screen are due to file not found or bad file type Quote Link to comment https://forums.phpfreaks.com/topic/79530-downloading-problem/#findComment-402885 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.