File Upload using PHP
To upload a file, we need a upload form with three basic parts.
First one ,a hidden filed to specify maximum size of the file.
Second field allow user to browse or type the name of the file.
And third one a submit button.
An example of such form is given below.
<form name=”upload_form” method=”post” action=”upload.php”> <input type="hidden" name="MAX_FILE_SIZE" value="1000" /> <input name="upload" type="file" /> <input type=”submit” value=”upload”/> </form>
After submitting the file php will upload the file to a desired directory.
PHP use $_FILE array, move_uploaded_file and basename functions for uploading.
If we are considering above form , after submitting the form, $_FILE['upload'] would have file information like name of the file and temporary name of the file. Where ‘upload’ is name of the file input filed
<input name="upload" type="file" />
move_uploaded_file function will upload the file, it needs two parameter . 1)temporary name of the file and 2 ) target path. move_uploaded_file returns true on success and false on failure.
A sample php code (upload.php) that uploads a file is given below.
<?php $target_path = “uploads/”. basename( $_FILES['upload']['name']); if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) echo “File uploaded successfully”; else echo “Unable to upload the file”; ?>




