May 25, 201115 yr Hey Guys, Does any one know how to display files from my FTP directories? I have managed to display files, but i also want to download them. The problem is it works on my local server, but when I run the PHP script on my hosting server, instead of downloading it , it display the file in the browser. I have even changed the Content-type in the header to the MIME type of the document, but it still doesnt work. I have attached the script for anyone to look into. Thanks everyone. <?php $name="hittesh"; //Current Users folder echo "<form method='get' action='$_SERVER[php_SELF]'>"; if ($handle = opendir($name)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filebase=basename($file); echo $dirname="/".$name."/".$filebase; echo "<a href='$_SERVER[php_SELF]?mode=download&file=$file'>$file</a> "; echo "<a href='$dirname'>$file</a> "; } if(isset($_GET['mode'])){ function getmimetype ($filename) { static $mimes = array( '\.jpg$|\.jpeg => 'image/jpeg', '\.gif => 'image/gif', '\.png => 'image/png', '\.html$|\.html => 'text/html', '\.txt$|\.asc => 'text/plain', '\.xml$|\.xsl => 'application/xml', '\.pdf => 'application/pdf', '\.doc => 'application/msword' ); foreach ($mimes as $regex => $mime) { if (eregi($regex, $filename)) return $mime; } // return 'application/octet-stream'; return 'text/plain'; } if (file_exists(getcwd()."/".$name."/".$_GET['file'])) { header('Content-Description: File Transfer'); header('Content-Type: ' . getmimetype($_GET['file'])); header('Content-Disposition: attachment; filename='.basename($_GET['file'])); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($_GET['file'])); ob_clean(); flush(); readfile(getcwd()."/".$name."/".$_GET['file']); exit; } } } echo "Outside".$file."<br/>"; closedir($handle); } echo "</form>"; $curr_user_folder=""; echo $_SERVER['PHP_SELF']."<br/>"; echo "S Name: ".$_SERVER['SERVER_NAME']; echo "CWD".getcwd() . "\n" ?>
June 3, 201115 yr The problem is it works on my local server, but when I run the PHP script on my hosting server, instead of downloading it , it display the file in the browser. Sounds like an issue with the headers you're using. Try using these headers instead: header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=\"".basename($_GET['file'])."\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize(getcwd()."/".$name."/".$_GET['file'])); readfile(getcwd()."/".$name."/".$_GET['file']); exit();
June 3, 201115 yr Author Sounds like an issue with the headers you're using. Try using these headers instead: header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=\"".basename($_GET['file'])."\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize(getcwd()."/".$name."/".$_GET['file'])); readfile(getcwd()."/".$name."/".$_GET['file']); exit(); Hi , thanks for your answer, but i used two things to make it work, ob_start() function and application/x-download. Thanks for the info . Regards, Hittesh
June 3, 201115 yr Author What about using SPL? - http://www.php.net/m...oryiterator.php Thanks, thats a really good resource.
Create an account or sign in to comment