Home  >  Article  >  Backend Development  >  How to upload php files to the server

How to upload php files to the server

王林
王林Original
2019-09-18 13:04:385496browse

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[&#39;DOCUMENT_ROOT&#39;]."/upload/";
$dest_file = $upload_path.basename($_FILES[&#39;myfile&#39;][&#39;name&#39;]);
if(move_uploaded_file($_FILES[&#39;myfile&#39;][&#39;tmp_name&#39;],$dest_file)){
          echo "文件已上传至服务器根目录的upload目录下";}else{
          echo "上传错误".$_FILES[&#39;myfile&#39;][&#39;error&#39;];}?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn