adam291086 Posted January 10, 2008 Share Posted January 10, 2008 i have this function. function GetFile() { $filename = $_GET['currFile']; $filename = str_replace("/adam","",$filename); $filename = "http://www.adamplowman.co.uk".$filename; echo $filename; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // addition by Jorg Weske $file_extension = strtolower(substr(strrchr($filename,"."),1)); if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); } I alwasy get the error message File not found. USE force-download.php? I copy and past the link manually and i get the file displayed. Any one got any ideas. I got the script from http://elouai.com/force-download.php Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/ Share on other sites More sharing options...
helraizer Posted January 10, 2008 Share Posted January 10, 2008 Do you get this error? ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath Sam Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435484 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 yep i do Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435486 Share on other sites More sharing options...
rajivgonsalves Posted January 10, 2008 Share Posted January 10, 2008 $filename = $_GET['currFile']; $filename = str_replace("/adam","",$filename); $filename = "http://www.adamplowman.co.uk".$filename; echo $filename; did you add these lines the script uses file_exists and readfile they can be used only on a local system not to get from http protocol Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435488 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 i added those line to get the file location instead of using the $_GET['file'] like the tutorial said Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435489 Share on other sites More sharing options...
helraizer Posted January 10, 2008 Share Posted January 10, 2008 <?php if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; ?> The error is because the file path wasn't given; so you either just went to www.mysite.com/force-download.php - therefore none specified. Sam Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435494 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 Yeah i know that but the file path is correct. Like i said I copy and past the link manually and i get the file displayed What should the $filename look like ? at the momement it looks like www.adamplowman.co.uk/current/adam.txt Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435496 Share on other sites More sharing options...
rajivgonsalves Posted January 10, 2008 Share Posted January 10, 2008 it should be your system path $_SERVER['DOCUMENT_ROOT']."/adam/adam.txt"; something like that... Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435499 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 oh right oooopppppssss Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435500 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 right i have changed my script to $filename = $_GET['currFile']; $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo $filename; $filename no looks like /homepages/12/d214897219/htdocs/adam/ftp/adam/cycling/images/header_01.jpg But i am still getting the same error Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435505 Share on other sites More sharing options...
rajivgonsalves Posted January 10, 2008 Share Posted January 10, 2008 how does your current file come in.... Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435519 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Author Share Posted January 10, 2008 well i am trying to build a simple ftp script. You navigate throught the direcorties and when you click on a file the url becomes Comand = getfile. This calls upon my function. Heres the script below. <?php error_reporting(E_ALL); $command = ""; if(isset($_GET["command"])) $command = $_GET["command"]; else $command = "listFiles"; switch($command) { case "listFiles": ShowFiles(); break; case "getFile": GetFile(); break; case "putFile": PutFile(); break; case "renameFile": RenameFile(); break; case "deleteFile": DeleteFile(); break; default: ShowFiles(); } function DoConn() { //server info $ftpServer = "87.106.169.32"; $ftpUser = "u46180240"; $ftpPass = "CusWsc.5"; //connect and check login details $c = @ftp_connect($ftpServer); $l = @ftp_login($c, $ftpUser, $ftpPass); if(!$c) die("A connection to $ftpServer couldn't be established"); else if(!$l) die("Your login credentials were rejected"); else return $c; } //show home directory function ShowFiles() { $conn = DoConn(); $currDir = ""; // Get the name of the current directory if home one is clicked if(isset($_GET["currDir"])) $currDir = $_GET["currDir"]; else $currDir = ftp_pwd($conn); // Retrieve a list of files and directories $fList = @ftp_nlist($conn, $currDir); // We will work out the parent directory $parentDir = strrev($currDir); $parentDir = ereg_replace("^[a-zA-Z0-9\-]*/", "", $parentDir); $parentDir = strrev($parentDir); ?> <a href='ftp1.php?command=listFiles&currDir=<?php echo $parentDir; ?>'> </a> <br> <?php //loop thrugh array of files and folders for($i = 0; $i < sizeof($fList); $i++) { // We will remove the parent directory (if any) // from the name of the file that gets displayed $trimFile = ereg_replace("^$currDir", "", $fList[$i]); // Remove any forward slash at front of name $trimFile = ereg_replace("^/", "", $trimFile); if(@ftp_chdir($conn, $fList[$i])) { @ftp_cdup($conn); ?> <a href='ftp1.php?command=listFiles&currDir=<?php echo $fList[$i]; ?>'> <?php echo $trimFile; ?> </a> <br> <?php } else { ?> <a href='ftp1.php?command=getFile&currFile=<?php echo $fList[$i]; ?>&currDir=<?php echo $currDir; ?>'> <?php echo $trimFile; ?> </a> <br> <?php } } } function GetFile() { $filename = $_GET['currFile']; $filename= $_SERVER['DOCUMENT_ROOT'].$filename; echo $filename; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // addition by Jorg Weske $file_extension = strtolower(substr(strrchr($filename,"."),1)); if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-435522 Share on other sites More sharing options...
rajivgonsalves Posted January 11, 2008 Share Posted January 11, 2008 Is the path correct ? for the file Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436238 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Author Share Posted January 11, 2008 this is the path /kunden/homepages/12/d214897219/htdocs/adam/adam/cycling/images/header_01.jpg which looks correct. Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436320 Share on other sites More sharing options...
rajivgonsalves Posted January 11, 2008 Share Posted January 11, 2008 try this in your browser http://[your-site-url]/adam/adam/cycling/images/header_01.jpg and see if it comes Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436321 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Author Share Posted January 11, 2008 ok i have got the path working problem now is i get the error message Warning: Cannot modify header information - headers already sent by (output started at /homepages/12/d214897219/htdocs/adam/ftp/ftp1.php:114) in /homepages/12/d214897219/htdocs/adam/ftp/ftp1.php on line 155 The error is repeated for the lines 147 - 155. Then at the bottom of the page i get 'E¼K]ã{"K|¦µöjÕó“õ›*z«”Ò¾â€ôÜ0|`•r7{ÇåíöµSÀÍÚŠl¬¢ßK «%½ääq* ÁïíU¸û@Ì_oi´pÇê@æ ¸ÜV}Q çlØÜ£úLÅS¥cKS¢³¨")c„#U´~S6N®`Æ X^r™QÆ.^ƒˆ‰‹c2k#”Éý¬r#p#ŒÒ‘§ÂRŨ™¢UÀ™Ù”‘µz™¸ÈƒÔËã5÷ÆUD͘͜‹ã4¾0@fý´ÖXÈ?0uñ„v¶wn?~0 ¾£+$’Äz%„’D„°’H†XOD’@d’HÿÐb>gÎI#CØeÚ¿7œp8I$—îÚs”oí?ÿŒ’F€÷oÿPÿ ̤É$×Á özÎt?´³wú¿ÂI&vÛéŠ7™$‚ÀžI³á I)`—’K $ƒa.²I$ [0i$‰å3~I$Œ½Rü„’Hy)`ºKÉ$ÑË,ÐI$@z#ÃÿrïôÆI ÿÙ Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436322 Share on other sites More sharing options...
rajivgonsalves Posted January 11, 2008 Share Posted January 11, 2008 what is on line 155 ? Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436324 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Author Share Posted January 11, 2008 all 9 errors are on the header coding This is line 155 header("Content-Length: ".filesize($filename)); Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436325 Share on other sites More sharing options...
adam291086 Posted January 11, 2008 Author Share Posted January 11, 2008 It was because of the echo before the header. I read the locked topic on headers. Quote Link to comment https://forums.phpfreaks.com/topic/85346-solved-force-a-download/#findComment-436352 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.