Home >Backend Development >PHP Tutorial >How to upload files to a specified location in php (code attached)
The content of this article is about how to upload files to the specified location in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
test.php (front-end code)
<!DOCTYPE html> <html> <head> <title>文件上传测试</title> <meta charset="utf-8"> </head> <body> <form action="do.php" method="post" enctype="multipart/form-data"> <input type="file" name="img"> <input type="submit" value="上传文件"> </form> </body> </html>
Explanation:
methodmust# under the from tag ##For post
<?php print_r($_FILES); ?>Explanation: print_r() Output the data passed by the form into an arrayThe output is:
Array ( [img] => Array ( [name] => 1.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\phpFF07.tmp [error] => 0 [size] => 17164 ) )
<?php print_r($_FILES); $filename=$_FILES['img']['name']; $type=$_FILES['img']['type']; $tmp_name=$_FILES['img']['tmp_name']; $size=$_FILES['img']['size']; $error=$_FILES['img']['error']; //将服务器上的临时文件移动到指定目录下 move_uploaded_file($tmp_name, "img/".$filename);$_FILES is the file upload variable, which stores the file upload datamove_uploaded_file($tmp_name,"specified folder name".$filename); Move the temporary files on the server to the specified directory, return true if successful, false if failedMethod 2:
copy($tmp_name, "img/".$filename);Related recommendations:
How to correctly formulate PHP Upload large file settings
# Implement php to upload pictures to the specified location and save the path to the database
The above is the detailed content of How to upload files to a specified location in php (code attached). For more information, please follow other related articles on the PHP Chinese website!