Click Here to view DEMO

This code can be used upload files to an available folder called "upload".
After uploading a file,it can be downloaded.Lets have a look in to codes... :)

This is the interface for the user to upload a file.

The following is used to create the above interface.

upload.html

<form enctype="multipart/form-data" action="upload.php" method="POST">Please choose a file: <input name="uploaded" type="file" /><br /><input type="submit" value="Upload" /></form>



upload.php

<?php//here im uploading files into a folder named "upload".you can change it according to //your uploading path$target = "upload/";$target = $target . basename( $_FILES['uploaded']['name']) ;$ok=1;//This is our size conditionif ($uploaded_size > 350000) {echo "Your file is too large.<br>";$ok=0;}//This is our limit file type conditionif ($uploaded_type =="text/php") {echo "No PHP files<br>";$ok=0;}//Here we check that $ok was not set to 0 by an errorif ($ok==0) {Echo "Sorry your file was not uploaded";}//If everything is ok we try to upload itelse {if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {echo "<div align='center'>The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded<br>";echo "<a href='download.php?target=".$target."'>Download Uploaded File</a><br>";echo "<a href='http://www.nimesha.comyr.com/projects/FileUpload/upload.html'>Upload Another File</a></div>";}else {echo "Sorry, there was a problem uploading your file.";}}



After uploading a file,the following interface will be displayed.

The uploaded file can be download using the link "Download Uploaded File".

download.php

<?php$target = $_REQUEST['target'];$fullPath = $target;if ($fd = fopen ($fullPath, "r")) {$fsize = filesize($fullPath);$path_parts = pathinfo($fullPath);header("Content-type: application/octet-stream");header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");header("Content-length: $fsize");header("Cache-control: private"); //use this to open files directlywhile(!feof($fd)) {$buffer = fread($fd, 2048);echo $buffer;}}fclose ($fd);exit;