Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP File / Directories Navigation

Featured Replies

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"

     	?>

  • 2 weeks later...

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();

  • 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

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.