- Check the file if it is allowed for upload or not. We can check for file size, file extension, file name etc.
- Copy the file to server.
- Place the file in required directory and then give necessary file permission.
Let us add the html code to show the browse button for the visitors to point to file required for uploading. Here is the code.
Code: Select all
<form enctype="multipart/form-data" action="_URL_" method=post>
Upload this file: <input name="userfile" type="file">
<input type="submit" value="Send File"></form>
Now let us go to the php part to handle the uploaded file. We have In PHP 3, the following variables will be defined within the destination script upon a successful upload, assuming that register_globals is turned on in php.ini. If track_vars is turned on, they will also be available in PHP within the global
array $HTTP_POST_VARS. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example above ( inside the form):
- $userfile - The temporary filename in which the uploaded file was stored on the server machine.
- $userfile_name - The original name or path of the file on the sender's system.
- $userfile_size - The size of the uploaded file in bytes.
- $userfile_type - The mime type of the file if the browser provided this information. An example would be "image/gif".
Let us check the file size and we will not allow file size more than 250 KB to get uploaded to our server. Here we are using a flag $file_upload to false for processing the file upload.
Code: Select all
if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$file_upload="false";}
Code: Select all
if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}
Code: Select all
$add="upload/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.
Code: Select all
if(move_uploaded_file ($userfile, $add)){
// do your coding here to give a thanks message or any other thing.
}else{echo "Failed to upload file Contact Site admin to fix the problem";}