Upload and Download files in php

Hey  this  is upload and download in  files in php. it's simple coding to  download  all files to download any files ex:images,excel,ppt,pdf and  this code more  highly used for images download .u can upload and download very easily. u need files folder in  folder.  u can upload   files  moves to  files folder u can need files folder in folder. u needed database to store the upload  file name  and download  to click a file name to  download automatically . i'm bring  to  link to download.php  file this is automatically download files. u no need to change the download files ...,


         Download

   
  1. 1st  create a database   database name - demo
  2. 2nd create table  table name - upload and create  column  id and name.
  3. finally run  this file and upload and download a  files and images  
    

Source Code:

Index.php


<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("demo",$conn) or die(mysql_error());
if(isset($_POST['submit'])!=""){
$name=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
move_uploaded_file($temp,"files/".$name);
$insert=mysql_query("insert into upload(name)values('$name')");
if($insert){
header("location:index.php");
}
else{
die(mysql_error());
}
}
?>
<html>
<head>
<title>Upload and Download</title>
</head>
<style>
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
a{color:#666;}
#table{margin:0 auto;background:#333;box-shadow: 5px 5px 5px #888888;border-radius:10px;color:#CCC;padding:10px;}
#table1{margin:0 auto;}
</style>
<body>
<h2><a href="http://crackerworld.blogspot.in/">Cracker World</a></h2>
<h3><p align="center">Files Upload And Download</p></h3>
<form enctype="multipart/form-data" action="" name="form" method="post">
<table border="0" cellspacing="0" cellpadding="5" id="table">
<tr>
<th >Chosse Files</th>
<td ><label for="photo"></label><input type="file" name="photo" id="photo" /></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="submit" id="submit" value="Submit" /></th>
</tr>
</table>
</form>
<br />
<br />
<table border="1" align="center" id="table1" cellpadding="0" cellspacing="0">
<tr><td align="center">Click to Download</td></tr>
<?php
$select=mysql_query("select * from upload order by id desc");
while($row1=mysql_fetch_array($select)){
$name=$row1['name'];
?>
<tr>
<td width="300">
<img src="tick.png" width="14" height="14"><a href="download.php?filename=<?php echo $name;?>"><?php echo $name ;?></a>
</td>
</tr>
<?php }?>
</table>
</body>
</html>

Download.php


<?php
function output_file($file, $name, $mime_type='')
{
if(!is_readable($file)) die('File not found or inaccessible!');

$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
"php" => "text/plain"
);
if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
};

@ob_end_clean();


if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024);
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($file, $range);

while(!feof($file) &&
(!connection_aborted()) &&
($bytes_send<$new_length)
)
{
$buffer = fread($file, $chunksize);
print($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else

die('Error - can not open file.');
die();
}
set_time_limit(0);
$file_path='files/'.$_REQUEST['filename'];
output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain');
?>

26 comments:

  1. Replies
    1. remove it...friend..it's no need

      Delete
  2. GREAT SCRIPT!!! Maybe the tutorial a bit longer, cause it was quite hard :S

    ReplyDelete
  3. how to download pdf file as table format

    ReplyDelete
  4. I'm using your code, but I want a second colum added in my download table, so their is alittle bit more information.

    ReplyDelete
  5. Hi my all files are get displayed but after click on the files the File not found or inaccessible! error is displayed.

    Please help

    ReplyDelete
  6. Dear Sir,

    Please help

    ReplyDelete
  7. Dear Satya Sir,

    please help

    ReplyDelete
  8. Sir! File not found or inaccesible.. Please Help

    ReplyDelete
    Replies
    1. if not file not found it's display's ( File not found or inaccessible ) upload files is missing

      Delete
    2. I'm also having the same problem of displaying the the uploaded files since it keeps saying that files are not found or inaccessible even if there were files actually uploaded.

      Delete
  9. Replies
    1. hello sir please give me the php code to upload multiple file and how to display all the currently uploaded and already uploaded file on a webpage and how to give a download button to each file of them which are displaying on the webpage
      please please please help me
      if u can do it fast then its beeter for me
      becouse i have to show my project on 6th of march
      my email is - pankajkumawat125@gmail.com
      please sir.

      Delete
    2. Send your code at gpandiyan.tech@gmail.com

      Delete
    3. sir, Can I get the code too, I need it ,please help!

      Delete
    4. gonggongqiangqiang@gmail.com please help

      Delete
  10. i have sent my php code at "gpandiyan.tech@gmail.com"
    sir please rply soon

    ReplyDelete
  11. im sorry for disturbing u sir,, but i need the code to view the upload file into my website..can u please attach me the code?

    ReplyDelete
  12. File not found or inaccessible!

    ReplyDelete
  13. hai i'm geting this type of error wat can i do....help me"File not found or inaccessible!"

    ReplyDelete
  14. I hav manged to upload files to server.but stil cant manage to download files.showing file is not found.please do help.thankyou in advance

    ReplyDelete
  15. Amazing tutorial, thanks for sharing :)

    ReplyDelete
  16. hi.. how can i add another column just to display the id?

    ReplyDelete