Jump to content

[SOLVED] Force a Download


adam291086

Recommended Posts

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

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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 ÿÙ

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.