Thursday 16 June, 2011

Lesson 12 PHP file download Script

Retweet this button on every post blogger
File download in PHP is quite easy we have to just scan our directory from which we want to download files
this procedure could be understand more easily by this example;

here we assume that the folder(directory) we want to download file is uploaded_files/ ,


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File download in PHP</title>
</head>

<body>
<h1>PHP file download.</h1>
<table cellpadding="5" cellspacing="5">
<thead><th>File</th><th>Download</th></thead>
<?php
$path = "uploaded_files/"; // the path for the folder.
if (file_exists($path))
 {
    $files = scandir($path);
for($i=2; $i<sizeof($files); $i++)
{
  ?>
  <tr><td><?php echo $files[$i] ?></td><td><a href="<?php echo $path.$files[$i]; ?>"><img src="dl.gif" /></a></td></tr>
  <?php
 }
}
?>
</table>
</body>
</html>
This script simply output a table with file name and download image.