Products
Services
Contact Us

Scripts: PHP :: file upload operations >> Library Article #1

Developer's Section

Upload a File Using PHP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (1907)
Favorites:

Learn simpe techniques to upload a file using PHP.

In this script tutorial we will learn how to upload a file using php.

Let's start with the php:

 Code Snippet 1

<? //upload a file in php

    $uploadError = "";

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

    $temp_name = $_FILES['uploadedfile']['tmp_name'];

    if(basename( $_FILES['uploadedfile']['name']) == "")
    {
        $uploadError = "There was an error processing your request.              
        $uploadError .= "Please Try Again!";
        
        exit;
    }

    if(file_exists($target_path))
    {
        $uploadError = "The File: ";
        $uploadError .= basename( $_FILES['uploadedfile']['name']);
        $uploadError .= " already exists in the system";

        exit;
    }
       
    if(move_uploaded_file($temp_name, $target_path)) 
    {
        //success        
        echo "File:" . $_FILES['uploadedfile']['name'] . "<br />";
        echo "Size:" . $_FILES['uploadedfile']['size'] . "<br />";
        echo " has been uploaded";
    }
?>


After you have the php in place you can create a simple form like this:

 Code Snippet 2

<form name="upload_file" enctype="multipart/form-data"  
 method="POST" action="your_script.php">
<table border="0">
   <tr>
       <td width="50">File:</td>
       <td><input name="uploadedfile" type="file" /></td>
   </tr>
   <tr>
       <td colspan="2">
       <input type="Submit" name="Submit" value="Upload">
       </td>
   </tr>
</table>
</form>


if you want to increase the max file size for eveydownload you can add the following input field to the form described above:

 Code Snippet 3

<!-- put this inside the form tags -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />


See other Scripts in file upload operations

Submit Your Scripts:

If you would like to have your PHP scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]