Home  >  Article  >  Backend Development  >  Chapter 13 Uploading Files_PHP Tutorial

Chapter 13 Uploading Files_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:59877browse

Learning points:
1.PHP upload configuration
2.$_FILES array
3.PHP upload function

Although most people think of the Web as only consisting of web pages, the HTTP protocol can actually transfer any file, such as office
documents, PDFs, executables, AVIs, compressed files, and various other file types. While FTP has historically been the standard way to upload files to servers, uploading files via the web is also becoming increasingly popular.

1. PHP upload configuration

There are some configuration directives that can be used to fine-tune PHP's file upload functionality. These directives are used to determine whether

PHP's file upload is enabled, the maximum allowed upload file size, the maximum allowable script memory allocation, and various other important
resources.
1.file_uploads=on|off: Determine whether the PHP script on the server can accept file uploads.
2.max_execution_time=integer: The maximum time, in seconds, that a PHP script can execute before registering a fatal error
.
3.memory_limit=integer: Set the maximum memory that the script can allocate, in MB. This prevents
runaway scripts from monopolizing server memory.
4.upload_max_filesize=integer: Set the maximum size of uploaded files in MB. This directive must be
smaller than post_max_size.
5.upload_tmp_dir=string: Set the uploaded file to be stored in a temporary location on the server before processing,
until the file is moved to the final destination.
6.post_max_size=integer: Determine the maximum size of information that can be accepted through the POST method, in MB as
unit.

2. $_FILES array

HTML of upload form

<span <</span><span form </span><span enctype</span><span ="multipart/form-data"</span><span  action</span><span ="upload.php"</span><span  method</span><span ="post"</span><span ></span>
<span <</span><span input </span><span type</span><span ="hidden"</span><span  name</span><span ="MAX_FILE_SIZE"</span><span  value</span><span ="1000000"</span> <span /></span><span 
上传文件: </span><span <</span><span input </span><span type</span><span ="file"</span><span  name</span><span ="userfile"</span> <span /></span>
<span <</span><span input </span><span type</span><span ="submit"</span><span  value</span><span ="上传"</span> <span /></span>
<span </</span><span form</span><span ></span>
ENCTYPE="multipart/form-data", this is the fixed writing method, otherwise the file upload will fail

ACTION="upload.php", define the path of the program file to be processed
METHOD="post", Define the transmission method as POST. Generally, Form submission data is set to POST

, this is a hidden field that

defines the upper limit of uploaded file size. When this value is exceeded, the upload fails. It must be defined in front of the file upload domain.
And the value defined here cannot exceed the value set by upload_max_filesize in the php.ini file, otherwise it will be meaningless. (Note: The value of MAX_FILE_SIZE is only for the browser A suggestion, in fact it can be bypassed by simply
so don't rely on browser restrictions. In fact, the maximum value for uploading files in PHP.ini settings is. It won't work. But it's better to add MAX_FILE_SIZE to the form, because it can avoid
the trouble of users spending time waiting to upload a large file only to find that the file is too big)
765e7db038108abbecd1bf54d7f8361b, this is the file upload domain, the Type attribute must be set to file, but the Name attribute
can be customized, this value will be used in the code file.


$_FILES super global variable, which stores various upload-related information, which is crucial for files uploaded to the server through PHP scripts

.

1. The value stored in the $_FILES["userfile"]["tmp_name"] variable is the location where the file is temporarily stored

in the Web server.
2. The value stored in the $_FILES["userfile"]["n name"] variable is the file name in the user system.
3. The value stored in the $_FILES["userfile"]["size"] variable is the byte size of the file.
4. The value stored in the $_FILES["userfile"]["type"] variable is the MIME type of the file, for example: text/plain
or image/gif.
5. The value stored in the $_FILES["userfile"]["error"] variable will be any error code related to file upload.
This is a new feature added in PHP4.2.0. error provides some array constants respectively: 0: means no error occurred, 1:
means the size of the uploaded file exceeds the agreed value. The maximum file size is specified in the PHP configuration file, the directive
is upload_max_filesize. 2: Indicates that the uploaded file size exceeds the maximum value specified by the MAX_FILE_SIZE element of the HTML form. 3: Indicates that the file is only partially uploaded. 4: Indicates that no files are uploaded.



<?<span php
</span><span print_r</span>(<span $_FILES</span><span );
</span>?>
3. PHP upload function

PHP's file system library provides a large number of file processing functions. In addition, PHP also provides two functions dedicated to the file upload process: is_uploaded_file() and move_uploaded_file(). 1. Determine whether to upload the file: is_uploaded_file()


2. Move uploaded files: move_uploaded_file()

<?<span php
</span><span if</span> (<span is_uploaded_file</span>(<span $_FILES</span>["userfile"]["tmp_name"<span ])) {
    </span><span echo</span> '已经上传到临时文件夹'<span ;
} </span><span else</span><span  {
    </span><span echo</span> '失败'<span ;
}
</span>?>
Note: The article comes from Li Yanhui’s PHP video tutorial. This article is for communication only and may not be used for commercial purposes, otherwise you will be responsible for the consequences.

<?<span php
</span><span if</span> (!<span move_uploaded_file</span>(<span $_FILES</span>["userfile"]["tmp_name"],<span $_FILES</span>["userfile"]["name"<span ])) {
    </span><span echo</span> '移动失败'<span ;
    </span><span exit</span><span ;
}
</span>?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/759626.htmlTechArticleLearning points: 1.PHP upload configuration 2.$_FILES array 3.PHP upload function Although most people think that Web Contains only web pages, but the HTTP protocol can actually transfer any file, such as office documents...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn