Introduction to the PHP file

Source: Internet
Author: User
Tags php file upload
Introduction to PHP File upload

?


The 39th chapter of the PHP manual has instructions for dealing with these issues, which I copy and paste as follows:

--------------------------------------------

Chapter 39? File upload Processing
Directory
POST? Method upload
Error message Description
Common defects
Uploading Multiple files
Right? Support for PUT? methods
POST? Method upload
This feature enables users to upload text and binary files. Use? PHP certification and file manipulation functions, you can fully control who is allowed to upload and how to deal with the file after uploading.

PHP? can accept any from the compliance? RFC-1867? Standard browser (including? Netscape? Navigator?3? and later versions, patched? Microsoft? Internet? Explorer?3? or later) uploaded files.?

Related settings:? See php.ini? file_uploads,upload_max_filesize,upload_tmp_dirpost_max_size? and Max_input_time? SET options.?

Please note? PHP? also supported? File upload for PUT? method, Netscape? The Composer? The "the"? Amaya? The client uses this method. See the right? Support for the PUT method to get more information.


Example? 39.1.? File Upload Form

You can create a special form to support file uploads as follows:?

????




????
The __url__ in the above example should be replaced, pointing to a real one? PHP? file.?

Max_file_size? The hidden Field (in bytes) must precede the file input field with a value of the maximum size of the received file. This is a recommendation to the browser, PHP? will also check this item. This setting can be bypassed easily on the browser side, so do not expect this feature to block large files. In fact, the maximum number of uploaded files in PHP settings is not invalidated. However, it is best to add this item to the form because it avoids the hassle of having to wait for a large file to be uploaded before the user discovers that the file is too large to upload.




Note: To ensure that the file upload form's properties are? enctype= "Multipart/form-data", otherwise the file cannot be uploaded.

Global variables? $_files? php?4.1.0 is present (used in earlier versions $HTTP _post_files?). This array contains all the uploaded file information.

In the example above? The contents of the $_files array are as follows. We assume that the name of the file upload field is as shown in the previous example, UserFile. Names can be arbitrarily named.

$_files[' UserFile ' [' Name ']
The original name of the client machine file.

$_files[' userfile ' [' type ']
The file? MIME? type, if the browser provides this information. An example is "Image/gif". But this? MIME? type in? PHP does not check, so do not assume that there is this value.

$_files[' userfile ' [' Size ']
The size of the uploaded file, in bytes.?

$_files[' UserFile ' [' Tmp_name ']
Temporary file names stored on the server after the files have been uploaded.

$_files[' userfile ' [' Error ']
The error code associated with the file upload. What is this item in? Added in php?4.2.0 version.?


Once the file is uploaded, it will be stored in the default temp directory on the server by default, unless the php.ini. Upload_tmp_dir? is set to a different path. Can the default temp directory on the server be changed? PHP? Environment variables for the run environment? TMPDIR? To reset, but in? The PHP script is not working internally by running the putenv () function to set it. This environment variable can also be used to confirm that other operations are also performed on the uploaded file.

Example? 39.2.? Make File Upload effective

Please refer to the function? Is_uploaded_file ()? and? Move_uploaded_file () for further information. The following example processes file uploads that are provided by the form.

//? In? php?versions?earlier?than?4.1.0,? $HTTP _post_files?should?be?used?instead
? of?$_files.

$uploaddir? =? ' /var/www/uploads/';
$uploadfile? = $uploaddir?.? basename ($_files[' userfile ' [' name ']);

Echo? '

';
If? (Move_uploaded_file ($_files[' userfile ' [' tmp_name '],? $uploadfile))? {
???? Echo? " File?is?valid,?and?was?successfully?uploaded.\n ";
}?else? {
???? Echo? " Possible?file?upload?attack!\n ";
}

Echo? ' Here?is?some?more?debugging?info: ';
Print_r ($_files);

Print? "
";

?



Accept the upload file? PHP? script in order to decide what to do next to the file, you should implement any logically necessary checks. For example, can you use the $_files[' userfile ' [' size '] variable to exclude files that are too large or too small, or to pass a. $_files[' userfile ' [' type ']? variable to exclude files of a file type and a file that does not conform to a standard, But just consider this as the first step in a series of checks, because this value is completely controlled by the client. PHP-end does not check. Self -? php?4.2.0, you can also use the $_files[' userfile ' [' ERROR '] variable to plan what to do next with different error codes. Anyway, either remove the file from the temp directory or move it somewhere else.

If a file is not selected for uploading in the form, then? PHP variable? $_files[' UserFile ' [' Size ']? The value will be ' 0,$_files[' userfile ' [' tmp_name ']? will be empty.

If the file has not been moved elsewhere and has not been renamed, the file will be deleted at the end of the form request.

Example? 39.3.? Uploading a set of files

PHP? HTML? Array attributes even support file types.




???
Foreach? ($_files["Pictures"] [error "]?as $key? =>? $error)? {
???? If? ($error? = =? UPLOAD_ERR_OK)? {
???????? $tmp _name?=?$_files["Pictures" ["Tmp_name"] [$key];
???????? $name =?$_files["Pictures" ["Name"] [$key];
???????? Move_uploaded_file ($tmp _name,? " data/$name ");
????}
}
?>

?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.