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

How to upload php files to the server

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-26 11:56:013167browse

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[&#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 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