PHP: Upload Files to the Server

Contributor Icon Contributed by William_Wilson Date Icon September 30, 2008  
Tag Icon Tagged: PHP programming

With PHP, it is simple to upload any file to your server.


uploader.php

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}?>

The form used to apply this uploading:

<form enctype="multipart/form-data" action="uploader.php" method="POST"^gt;
<input type="hidden" name="MAX_FILE_SIZE" value="1000" />
Upload File: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>

I suggest implementing a password (php) protection on this page to ensure noone other than the administrator can upload files. Missuse of uploading can surpass a server’s space and cause fines or bans of site owners. So be careful.

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

Previous recipe | Next recipe |
 
  • Kinox
    Thanks for sharing the codes.

    Btw, there is this "File Thingie" script (free for personal use) that you may want to check out too.

    Quote: "It is a small web-based file manager written in PHP. It is intended for those who need to give others access to a part of their server's file system when FTP is not practical."

    Nope, I'm not related to the author. :)
  • it is good method to upload small files, but if we need to upload larg files then we need more complex code

    thanks
blog comments powered by Disqus