Home>Article>Backend Development> How to write php file upload code

How to write php file upload code

angryTom
angryTom Original
2019-10-26 16:29:32 2953browse

How to write php file upload code

How to write the php file upload code

When we first learned php, we couldn’t understand the php file upload code , as well as the upload logic, the following code is written from a beginner's perspective, I hope it will be helpful to everyone!

Required knowledge:

Super global array: the value of $_FILES

$_FILES['myfile']['name'] Is: The file name of the client file system

$_FILES['myfile']['type'] The value is: The file type passed by the client

$_FILES['myfile'] The value of ['size'] is: the byte size of the file

The value of $_FILES['myflie']['tmp_name '] is: the temporary full path stored on the server after the file is uploaded

The value of

$_FILES['myfile']['error'] is: the error code of file upload

The value stored in $_FILES['myfile']['error']

A value of 0: indicates that no error occurred.

A value of 1: indicates that the size of the uploaded file exceeds the agreed value. The maximum file size is specified in the PHP configuration file. The instruction is: upload_max_filesize

The value is 2: Indicates that the uploaded file size exceeds the maximum value specified by the MAX_FILE_SIZE element of the HTML form hidden field attribute

A value of 3: Indicates that the file was only partially uploaded

A value of 4: Indicates that no files were uploaded

A value of 6: Indicates that the temporary folder cannot be found

The value is 7: Indicates that the file writing failed.

The error value corresponds to the constant

UPLOAD_ERR_OK: The corresponding value is 0

UPLOAD_ERR_INI_SIZE: The corresponding value is 1

UPLOAD_ERR_FORM_SIZE: Corresponding value 2

UPLOAD_ERR_PARTIAL: Corresponding value 3

UPLOAD_ERR_NO_FILE: Corresponding value 4

UPLOAD_ERR_NO_TMP_DIR: Corresponding value 6

UPLOAD_ERR_CANT_WRITE: Corresponding Value 7

upload.html The code is as follows

    图片上传 

upload.php The code is as follows

 1024 * 1024 * 8) //以字节为主 1024*1024是1M { exit('上传的文件太大'); } //设置新文件名 $filename = date('YmdHis',strtotime('now')).rand(1000,9999); //获取上传文件的后缀名 $name = $file['pic']['name']; //得到文件名字符串 $filestr = explode('.',$name); $ext = array_pop($filestr); //拼接新文件名 $newfilename = $filename.'.'.$ext; //拼接上传文件的路径 $path = './uploads/'; //绝对路径 $abspath = $path.$newfilename; if(move_uploaded_file($file['pic']['tmp_name'],$abspath)) { echo '上传成功'; }else{ echo '上传失败'; } }

For more PHP related knowledge, please visitPHP Chinese website!

The above is the detailed content of How to write php file upload code. For more information, please follow other related articles on the PHP Chinese website!

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