In this simple source code, you can learn how to manage (upload/download) files using PHP/PDO. The simple project can store different types/formats of files and also the user can download the uploaded file. The source code can detect if the file name has duplicates and will automatically add a number for example if the filename "sample.pdf" already exists in the database the system will store the file as "sample_2.pdf" and if ever user will upload a file using the same filename the system will also check if the new filename has no duplicates. In addition to the example scenario, if the user will upload "sample.php", the system will change it into "sample_3".
<?php $name=$_FILES['file']['name']; $size=$_FILES['file']['size']; $type=$_FILES['file']['type']; $temp=$_FILES['file']['tmp_name']; $chk = $conn->query("SELECT * FROM upload where name = '$name' ")->rowCount(); if($chk){ $i = 1; $c = 0; while($c == 0){ $i++; $chk2 = $conn->query("SELECT * FROM upload where name = '$tname' ")->rowCount(); if($chk2 == 0){ $c = 1; $name = $tname; } } } if($move){ $query=$conn->query("insert into upload(name,fname)values('$name','$fname')"); if($query){ } else{ } } } ?>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example"> <thead> <tr> <th width="90%" align="center">Files</th> <th align="center">Action</th> </tr> </thead> <?php $query=$conn->query("select * from upload order by id desc"); while($row=$query->fetch()){ $name=$row['name']; ?> <tr> <td> <?php echo $name ;?> </td> <td> <button class="alert-success"><a href="download.php?filename=<?php echo $name;?>&f=<?php echo $row['fname'] ?>">Download</a></button> </td> </tr> <?php }?> </table>
<?php function output_file($file, $name, $mime_type='') { "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==''){ $mime_type=$known_mime_types[$file_extension]; } else { $mime_type="application/force-download"; }; }; { if(!$range_end) { $range_end=$size-1; } else { } $new_length = $range_end-$range+1; } else { $new_length=$size; } $chunksize = 1*(1024*1024); $bytes_send = 0; { ($bytes_send<$new_length) ) { print($buffer); } } else } $file_path='upload/'.$_REQUEST['f']; output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain'); ?>
That source code above are important script you will need in developing a simple Upload and Download File System. You can also use bootstrap for the design of your project. Feel Free to Copy and paste the source codes above and try develop a simple file upload and download system on your own. Also, you can download the working project source code below.
Happy Coding :)
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
On Index.php on line 10 change
move_uploaded_file($temp,"files/".$name);
to
move_uploaded_file($temp,"upload/".$name);
Download link is not working