Home > Article > Backend Development > How to upload files in php and save them locally
#How to upload files and save them locally in php?
It is very simple for php as a backend to accept uploaded images. You need to use FILES. When the client or web end posts images to the back end, we can use FILES. When the client or web end posts images to the back end, we can use FILES. When posting pictures from the end to the back end, we can use _FILE to receive the pictures, then store them in a temporary buffer, and finally use the move_upload_file function to save them locally. When using the Wampserver server, the cache files are placed in the tmp folder.
//html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> </head> <body> <form action="./uploadheadimg.php" method="post" enctype="multipart/form-data"> <!-- <input type="hidden" name="MAX_FILE_SIZE" value='176942' /> --> 请选择您要上传的文件:<input type="file" name='myfile' /> <!-- <input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br /> --> <input type="submit" value="上传文件" /> </form> </body> </html>
html interface display
##2.//uploadheadimg.php code<?php $imgname = $_FILES['myfile']['name']; $tmp = $_FILES['myfile']['tmp_name']; $filepath = 'photo/'; if(move_uploaded_file($tmp,$filepath.$imgname.".png")){ echo "上传成功"; }else{ echo "上传失败"; } ?>The value of the $filepath variable needs to be modified in the code , whose value creates a folder to store uploaded files. Recommendation: "
PHP Tutorial"
Attached is the common $_FILES system in the PHP programming language Function usage is: $_FILES['myFile']['name'] displays the original name of the client file. $_FILES[‘myFile’][‘type’] The MIME type of the file, such as "image/gif". $_FILES[‘myFile’][‘size’] The size of the uploaded file, in bytes. $_FILES[‘myFile’][‘tmp_name’] The name of the temporary file stored, usually the system default. $_FILES[‘myFile’][‘error’] The error code related to file upload. The following is the meaning of the error code when using $_FILES[‘myFile’][‘error’]: 0; The file was uploaded successfully. 1; The file size exceeds the size set by the system in php.ini. 2; The file size exceeded the value specified by the MAX_FILE_SIZE option. 3; Only part of the file was uploaded. 4; No files were uploaded. 5; The uploaded file size is 0.The above is the detailed content of How to upload files in php and save them locally. For more information, please follow other related articles on the PHP Chinese website!