note 63719 added to function.is-uploaded-file
| From: | tejas at kaushalam dot com | Date: | Wed, 29 Mar 2006 07:28:27 +0000 |
| Subject: | note 63719 added to function.is-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106924@lists.php.net to get a copy of this message | ||
<FORM ENCTYPE="multipart/form-data" ACTION="_URL_" METHOD=POST>
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>
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 php3.ini. If track_vars is turned on, they will also be available
in PHP 3 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:
* $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.<BR>";
$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<BR>";
$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";}
Thats all... the file is placed in our directory (name: upload)
----
Server IP: 203.199.107.94
Probable Submitter: 59.95.208.140
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.is-uploaded-file.php
Edit -- http://master.php.net/note/edit/63719
Del: integrated -- http://master.php.net/note/delete/63719/integrated
Del: useless -- http://master.php.net/note/delete/63719/useless
Del: bad code -- http://master.php.net/note/delete/63719/bad+code
Del: spam -- http://master.php.net/note/delete/63719/spam
Del: non-english -- http://master.php.net/note/delete/63719/non-english
Del: in docs -- http://master.php.net/note/delete/63719/in+docs
Del: other reasons-- http://master.php.net/note/delete/63719
Reject -- http://master.php.net/note/reject/63719
Search -- http://master.php.net/manage/user-notes.php