Home > Backend Development > PHP Problem > How to upload php files to the server

How to upload php files to the server

爱喝马黛茶的安东尼
Release: 2023-02-25 06:30:02
Original
3185 people have browsed it

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">
Copy after login

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>
Copy after login

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);
Copy after login

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;];}?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template