PHP: Upload Files to the server

Home -> Programming -> Web -> PHP

9965 views

From the computer of: William_Wilson (107 recipes)
Created: Jun 25, 2006     Updated: Jun 28, 2006


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

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">
   <input type="hidden" name="MAX_FILE_SIZE" value="1000" />
   Upload File: <input name="uploadedfile" type="file" /> <br />
   <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)

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  PHP: Delete or unlink a file on the server
  PHP: Rename or move a file on the server
  PHP: determine the directory path of the server root
  PHP: Set or create a simple browser cookie with setcookie
  Solve PHP Fatal error: Allowed memory size of 8388608 bytes exhausted (tried...
  PHP: Create a File on your Server
  Solve PHP error: Cannot modify header information - headers already sent
  PHP: Password Protect Your pages.
  PHP: Delete or expire a browser cookie
  PHP: Generate Dynamic Images from Text

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.