Problem with download system
Posted: Wed Aug 12, 2009 9:38 am
I have download system with php. Main file who is name script.php is:
Second file is download.php is:
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?
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>
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!');
}
?>