* 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.
Before starting we must add file permission to the directory where we plan to store the files after uploading it. Let us give the directory name upload and give write permission to it. If it is a Window server nothing is required and by default window gives all permissions. For Linux and Uinx we have to give write (Chmod) permission to allow uploaded files to store.
If you are uploading large files then read about maximum file execution time allowed and its adjuestments.
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
Upload this file:
This html code will display a text area with file upload button. The form tag is bit different than the normal form tag used ( see the encrypte =).
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".
With all these info we will make our script ready to handle the files.
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.
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".
With all these info we will make our script ready to handle the files.
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.
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.
";
$file_upload="false";}
";
$file_upload="false";}
Now let us check the file extension and only jpg or gif file pictures we will allow into our server. We will check this by using $userfile_type extension
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
";
$file_upload="false";}
We will limit ourselves to these two type checks and if we find our $file_upload variable is not "false" then we can upload the file. This part is very simple in PHP and can be done in one step. Before that let us decide the file directory name where we will be placing the file. $add is the path with the file name relative to the script running this code where the uploaded file will be stored.
";
$file_upload="false";}
We will limit ourselves to these two type checks and if we find our $file_upload variable is not "false" then we can upload the file. This part is very simple in PHP and can be done in one step. Before that let us decide the file directory name where we will be placing the file. $add is the path with the file name relative to the script running this code where the uploaded file will be stored.
$add="upload/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.
Now let us copy the file to server. The command move_uploaded_file will does that job for us and if the action is successful then it will return true.
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";}
No comments:
Post a Comment