Home > Article > Backend Development > How to upload php files to the server
File upload in PHP
Usually, file upload uses the HTTP POST method. First, you need to define the enctype attribute of the HTML form as "multipart/form-data".
<form enctype="multipart/form-data" action="somefile.php" method="POST">
Related recommendations: "php Getting Started Tutorial"
Upload page:
//HTML文件: <html> <head> <title>支持文件上传的HTML表单</title> </head> <body> <form enctype="multipart/form-data" action="4.php" method="POST"> 上传此文件:<input name="myfile" type="file" /> <input type="submit" value="提交上传" /> </form> </body> </html>
In the PHP program, use the global variable $_FILES to process file uploads . $_FILES is an array containing file information to be uploaded.
The file will be uploaded to the default path by default. If you need to upload it to the specified path:
move_uoloaded_file (filename,destination);
The php file that processes the file:
//4.php<?php$upload_path = $_SERVER['DOCUMENT_ROOT']."/upload/"; $dest_file = $upload_path.basename($_FILES['myfile']['name']); if(move_uploaded_file($_FILES['myfile']['tmp_name'],$dest_file)){ echo "文件已上传至服务器根目录的upload目录下";}else{ echo "上传错误".$_FILES['myfile']['error'];}?>
The above is the detailed content of How to upload php files to the server. For more information, please follow other related articles on the PHP Chinese website!