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 HTML form. The enctype attribute is "multipart/form-data".
<form enctype="multipart/form-data" action="somefile.php" method="POST">
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 handle file upload. $_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);
php file for processing 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 content is for reference only!
Recommended tutorial: PHP video tutorial
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!