Page 1 of 1

Problem with download system

Posted: Wed Aug 12, 2009 9:38 am
by tomek34
I have download system with php. Main file who is name script.php is:

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<title>Files Download</title>
</head>
<body>
<div>
 
<?php
 
 
$dir = "./downloads/";
function printDir($dir)
{
  $fd = opendir($dir);
  if(!$fd) return false;
  while (($file = readdir($fd)) !== false){
    if($file != "." && $file != ".."){
      echo("<a href=\"download.php?name=");
      echo("$file\">$file</a><br />");
    }
  }
  closedir($fd);
}
printDir($dir);
?>
 
</div>
</body>
</html>
 
Second file is download.php is:

Code: Select all

 
<?php
 
 
$filesPath = "./";
 
function securityCheck($name)
{
  $wyr = "^[a-z0-9_-]+(\.[a-z0-9_-]+)*$";
  return eregi($wyr, $name);
}
function send($fileName, $filePath)
{
  if(!file_exists($filePath.$fileName)){
    echo('File does not exit on Server!');
    return;
  }
  $fd = fopen($filePath.$fileName,"r");
  $size = filesize($filePath.$fileName);
  $contents = fread($fd, filesize($filePath.$fileName));
 
  fclose($fd);
 
  header("Content-Type: application/octet-stream");
  header("Content-Length: $size;");
  header("Content-Disposition: attachment; filename=$fileName");
 
  echo $contents;
}
if(isSet($_GET['name'])){
  if(!securityCheck($_GET['name'])){
    echo('File does not exit on Server!');
  }
  else{
    send($_GET['name'], $filesPath);
  }
}
else{
  echo('File does not exit on Server!');
}
?>
First file work ok and list everything from folder second when I click to dowlnoad show so File does not exit on Server! I can't find mistake. Can U help me?

Re: Problem with download system

Posted: Tue Aug 18, 2009 4:36 am
by mrvijayakumar
I don't know the exact problem in your code, Please try this, it will works in all browsers.

Code: Select all

<?php
$file = "../files/video.mpeg"; //Filename with path
$filename = "video.mpeg"; //Filename with extension to store in you PC
dwfile($file,$filename);
 
function dwfile($file,$filename) 
{ 
    if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) 
    { 
        // IE Bug in download name workaround 
        ini_set( 'zlib.output_compression','Off' ); 
    } 
    //header ('Content-type: ' . mime_content_type($filename)); 
    header ('Content-Disposition: attachment; filename="'.basename($filename).'"'); 
    header ('Expires: '.gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y"))).' GMT'); 
    header ('Accept-Ranges: bytes'); 
    header ('Cache-control: no-cache, must-revalidate'); 
    header ('Pragma: private'); 
 
    $size = filesize($file); 
    if(isset($_SERVER['HTTP_RANGE'])) 
    { 
        list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); 
        //if yes, download missing part 
        str_replace($range, "-", $range); 
        $size2=$size-1; 
        $new_length=$size2-$range; 
        header("HTTP/1.1 206 Partial Content"); 
        header("Content-Length: $new_length"); 
        header("Content-Range: bytes $range$size2/$size"); 
    } 
    else 
    { 
        $size2=$size-1; 
        header("Content-Range: bytes 0-$size2/$size"); 
        header("Content-Length: ".$size); 
    } 
 
    if ($file = fopen($file, 'rb')) 
    { 
        while(!feof($file) and (connection_status()==0)) 
        { 
            print(fread($file, 1024*8)); 
            flush(); 
        } 
        $status = (connection_status()==0); 
        fclose($file); 
    } 
    return($status); 
} 
 
 
?>