Home >Backend Development >PHP Problem >How to upload files in php and save them locally

How to upload files in php and save them locally

藏色散人
藏色散人Original
2019-11-13 11:02:325149browse

How to upload files in php and save them locally

#How to upload files and save them locally in php?

It is very simple for php as a backend to accept uploaded images. You need to use FILES. When the client or web end posts images to the back end, we can use FILES. When the client or web end posts images to the back end, we can use FILES. When posting pictures from the end to the back end, we can use _FILE to receive the pictures, then store them in a temporary buffer, and finally use the move_upload_file function to save them locally. When using the Wampserver server, the cache files are placed in the tmp folder.

//html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="./uploadheadimg.php" method="post" enctype="multipart/form-data">
<!-- <input type="hidden" name="MAX_FILE_SIZE" value=&#39;176942&#39; /> -->
请选择您要上传的文件:<input type="file" name=&#39;myfile&#39; />
<!-- <input type="file" name="myFile"  accept="image/jpeg,image/gif,image/png"/><br /> -->
<input type="submit" value="上传文件" />
</form>
</body>
</html>

html interface display

How to upload files in php and save them locally

##2.//uploadheadimg.php code

<?php
    $imgname = $_FILES[&#39;myfile&#39;][&#39;name&#39;];
    $tmp = $_FILES[&#39;myfile&#39;][&#39;tmp_name&#39;];
    $filepath = &#39;photo/&#39;;
    if(move_uploaded_file($tmp,$filepath.$imgname.".png")){
        echo "上传成功";
    }else{
        echo "上传失败";
    }
?>

The value of the $filepath variable needs to be modified in the code , whose value creates a folder to store uploaded files.

How to upload files in php and save them locally

Recommendation: "

PHP Tutorial"

Attached is the common $_FILES system in the PHP programming language Function usage is:

$_FILES['myFile']['name'] displays the original name of the client file.

$_FILES[‘myFile’][‘type’] The MIME type of the file, such as "image/gif".

$_FILES[‘myFile’][‘size’] The size of the uploaded file, in bytes.

$_FILES[‘myFile’][‘tmp_name’] The name of the temporary file stored, usually the system default.

$_FILES[‘myFile’][‘error’] The error code related to file upload.

The following is the meaning of the error code when using $_FILES[‘myFile’][‘error’]:

0; The file was uploaded successfully.

1; The file size exceeds the size set by the system in php.ini.

2; The file size exceeded the value specified by the

MAX_FILE_SIZE option.

3; Only part of the file was uploaded.

4; No files were uploaded.

5; The uploaded file size is 0.

The above is the detailed content of How to upload files in php and save them locally. 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