Home > Article > Backend Development > PHP image upload implementation code_PHP tutorial
Understand the files function first
The contents of the $_files array are as follows:
$_files['myfile']['name'] The original name of the client file.
$_files['myfile']['type'] The mime type of the file, which requires the browser to provide support for this information, such as "image/gif".
$_files['myfile']['size'] The size of the uploaded file, in bytes.
$_files['myfile']['tmp_name'] The temporary file name stored on the server after the file is uploaded, usually the system default. It can be specified in the upload_tmp_dir of the php tutorial.ini, but setting it with the putenv() function will not work.
$_files['myfile']['error'] Error codes related to the file upload. ['error'] was added in PHP 4.2.0 version. The following is its description: (They become constants after php3.0)
upload_err_ok
Value: 0; No error occurred and the file was uploaded successfully.
upload_err_ini_size
Value: 1; The uploaded file exceeds the limit of the upload_max_filesize option in php.ini.
upload_err_form_size
Value: 2; The size of the uploaded file exceeds the value specified by the max_file_size option in the html form.
upload_err_partial
Value: 3; Only part of the file was uploaded.
upload_err_no_file
Value: 4; No files were uploaded.
Value: 5; Upload file size is 0.
php code
$mkdir_file_dir = mkdir('./img/'.$_post['title'],0777); // Start creating a picture-related directory when uploading files
$tmp_file_name = $_files['file']['tmp_name']; //Temporary file name after successful upload
$file_name = $_files['file']['name']; //Original file name
$file_dir = './img/'.$_post['title'].'/'; //Assign the created directory to a variable as the final saving directory
If(is_dir($file_dir))
{
move_uploaded_file($tmp_file_name,$file_dir.$file_name); //Start moving files
}
?>
html code
my is upfile app!!
http-equiv="content-type" content="text/html;charset=utf-8" />
A very important point is: after the file is submitted through post, it is saved in the temporary folder of c:windowstemp. Through $_files["photo"]["tmp_name"], we can easily obtain the uploaded temporary file and save it To the path we specified, the following is the solution move_uploaded_file($_files["photo"]["tmp_name"],$path)