Php easily implements the file upload function. _ PHP Tutorial

Source: Internet
Author: User
Tags learn php programming temporary file storage
Php easily implements the file upload function ,. Php easily implements the file upload function. This article consists of five parts for analysis and explanation of php file uploads, the specific content is as follows: File Upload variables move temporary files on the server to php to easily implement the file upload function,

This article is divided into five parts for analysis and explanation of php file uploading. the specific content is as follows:

  • File upload variable
  • Move temporary files on the server to the specified directory
  • Php. ini Upload configuration
  • Error code
  • Single file Upload instance

1. file Upload variables

// $ _ FILES: file upload variable # name file name # type file type # tmp_name temporary file name # size file size # error message $ filename = $ _ FILES ["myFile"] ["name "]; $ type = $ _ FILES ["myFile"] ["type"]; $ tmp_name = $ _ FILES ["myFile"] ["tmp_name"]; $ size = $ _ FILES ["myFile"] ["size"]; $ error = $ _ FILES ["myFile"] ["error"];

2. move temporary files on the server to the specified directory

// 1. move_uploaded_file ($ tmp_name, $ destination): move the temporary file on the server to the specified directory # Name of the uploaded file. if the file is successfully moved, true is returned. otherwise, falsemove_uploaded_file ($ tmp_name, "D: /". $ filename); // 2. copy ($ tmp_name, $ destination) copy ($ tmp_name, "D :/". $ filename );

3. php. ini Upload configuration

# File_uploads = On support for HTTP upload # upload_tmp_dir = "" temporary file storage directory # upload_max_filesize = 2 M maximum number of files allowed to be uploaded # max_file_uploads = 20 maximum number of files allowed for one Upload # post_max_size = maximum size of data sent in 8 M post mode # max_execution_time = 1 sets the maximum execution time allowed before the script is terminated by the parser, the unit is seconds to prevent the program from writing poorly and occupy the server resources # max_input_time = 60 maximum time allowed by the script to parse the input data, unit: Seconds # max_input_nesting_level = 64 set the nested depth of input variables # max_input_vars = 1000 how many input variables are accepted # memory_limit = 128 M maximum single-thread independent memory usage

4. error code

#0. no error occurs. the file is uploaded successfully. #1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. #2. the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. #3. only part of the file is uploaded. #4. no files are uploaded. #6. the temporary folder cannot be found. #7. file writing failed. #8. the uploaded file is interrupted by the PHP extension.

5. Upload a single file to an instance
Form.html

 
  Document

FileUpload. php

Header ("content-type: text/html; charset: UTF-8"); $ fileInfo = $ _ FILES ["myFile"]; $ filename = $ fileInfo ["name"]; $ type = $ fileInfo ["type"]; $ error = $ fileInfo ["error"]; $ size = $ fileInfo ["size"]; $ tmp_name = $ fileInfo ["tmp_name"]; $ maxSize = 2*1024*1024; // maximum allowed value $ allowExt = array ("jpeg", "jpg ", "gif"); $ flag = true; // check whether the image type is true. // Determine the error code if ($ error = 0) {// determine the size of the uploaded file if ($ size> $ maxSize) {exit ("the uploaded file is too large");} // check the file type // Retrieve the file File extension $ ext = pathinfo ($ filename, PATHINFO_EXTENSION); if (! In_array ($ ext, $ allowExt) {exit ("invalid file type");} // checks whether the image type is true if ($ flag) {if (@! Getimagesize ($ tmp_name) {exit ("not a positive image type") ;}}// create a directory $ path = "D:/test/"; if (! File_exists ($ path) {mkdir ($ path, 0777, true); chmod ($ path, 0777) ;}// ensure that the file name is unique, prevents duplicate names from overwriting $ uniName = md5 (uniqid (microtime (true), true )). ". ". $ ext; $ destination = $ path. $ uniName; if (@ move_uploaded_file ($ tmp_name, $ destination) {echo "Upload successful" ;}else {echo "Upload failed" ;}} else {switch ($ error) {case 1: case 2: case 3: case 4: case 6: case 7: case 8: echo "upload error"; break ;}}

I hope this article will help you learn php programming.

Articles you may be interested in:
  • Php file Upload extension and file type table (covering almost all files)
  • PHP image file Upload implementation code
  • Php file upload code
  • Php file Upload instance code
  • ThinkPHP file upload example tutorial
  • PHP implements a complete example of video file Upload
  • How to use PHP and HTML5 FormData to upload a fresh object without refreshing
  • ThinkPHP combined with AjaxFileUploader to implement the method of uploading non-refreshing files
  • Implementation of Thinkphp multi-file Upload
  • A classic php file Upload class sharing

This article is divided into five parts for analysis and explanation of php file uploading. the specific content is as follows...

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.