Php implements file upload and download (on ). Php implements file upload and download (on) php implements file upload and download is a very basic function, generally the website will have such a demand, of course, it does not mean that all php files are uploaded and downloaded (on)
Php is a basic function to upload and download files. generally, websites have such requirements. of course, it does not mean that all files can be uploaded, the network is too insecure. Because I have not been familiar with php for a long time, I am writing a trainer today, and I am making a public record.
File upload: upload the client files to the server, and move the temporary files on the server to the specified directory. The main idea is to first define and obtain the two-dimensional array variables in $ _ FILES. you can use two-dimensional arrays each time, or you can put the first one into an array variable, then, each time you get the value in the array variable, the amount of code is simplified ----> If the error code is 0 or UPLOAD_ERR_ OK is true, no error is returned, you can upload -----> move the temporary files on the server to the specified directory. what is the name of the file? true is returned if the file is successfully moved. use move_uploaded_file ($ temporary file name, $ destination) -----> Another method is to use the copy ($ temporary variable name, $ destination) function;
$ _ FILES stores the information of the uploaded file. this is a two-dimensional array. The first dimension is the name of the uploaded file, and the second dimension is the information of the file, after uploading the file, you can print the parameters in the array to view it. use print_r ($ _ FILES) to see the following content:
Name: name of the uploaded file;
Type: MIME type of the uploaded file;
Tmp_name: temporary file name uploaded to the server; // It is very important, because this file will be manipulated on the server in the future.
Size: The size of the uploaded file;
Error: error code of the uploaded file. The value 0 indicates that the file is successfully uploaded ,.
Server configuration --- php. ini:
File_uploads = on, supports http upload;
Upload_tmp_dir =, Directory of temporary files;
Upload_max_filesize = 2 M; size of the file to be uploaded;
Max_file_uploads = 20; number of files allowed to be uploaded at a time. the default value is 20;
Max_execution_time =-1; sets the maximum number of execution seconds allowed before the script is parsed and terminated to prevent the program from occupying the server resources because it is too bad (such as an endless loop;
Max_input_time = 60; maximum number of seconds allowed for parsing input data by script;
Max_input_nesting_level = 64, set the nested depth of input variables;
Max_input_vars = 60; how many input variables are accepted to reduce the possibility of dos attacks. if the number of variables exceeds the specified number, it will lead to the generation of E_WARNING, more input variables will be truncated from the request;
Memory_limit = 128 M. The maximum independent memory usage of a single thread, that is, a web request, defines the maximum memory usage of the thread.
There are also some error information descriptions, which are basically the literal meaning of English. the temporary directory cannot be found, the size of which exceeds the limit, and so on. a total of 8 error messages correspond to numbers 0-8 but no numbers 5, respectively, in the code, you can determine the output, according to the one-to-one correspondence, you can use switch ...... Case, a total of 7 cases (0 indicates success, no need to write) to indicate output error information. Determine in the code. if there is no error output, upload the file.
Some client configurations-but the client configuration does not really have a limit in many cases, and the actual restriction is to be configured on the server:
Limit the maximum size of uploaded files by hiding fields in a form
Restrict the Upload file type through the accept attribute
In the course of this practice, I did not realize that the maximum upload limit of form forms (the determination in the program) and the determination of the underlying php (php. ini), so if the maximum size of my form upload is 2 MB, and php. the value in ini is 10 MB. when the size of the uploaded file exceeds 10 MB, a code error is Prompted. when the size of the uploaded file is between 2 MB and 10 MB, the program can execute, output the corresponding error information according to the determination.
Upload (on) php is a basic function to upload and download files. generally, websites may have such requirements, not all of them...