How to use php move_uploaded_file function

青灯夜游
Release: 2023-02-22 19:40:02
Original
3027 people have browsed it

The move_uploaded_file() function is used to move the uploaded file to a new location. The syntax is move_uploaded_file(file,newloc). If file is not a legal uploaded file, no operation will occur and false will be returned.

How to use php move_uploaded_file function

How to use the php move_uploaded_file() function?

Function: Move the uploaded file to a new location; if the target file already exists, it will be overwritten.

Syntax:

move_uploaded_file(file,newloc)
Copy after login

Parameters:

● file: required. Specifies the files to be moved.

● newloc: required. Specifies the new location of the file.

Return value: The function returns TRUE if successful, and FALSE if failed.

Note:

1. This function is only used for files uploaded through HTTP POST.

2. This function checks and ensures that the file specified by file is a legal upload file (that is, uploaded through PHP's HTTP POST upload mechanism). If the file is legal, it is moved to the file specified by newloc.

●If file is not a legal uploaded file, no operation will occur and move_uploaded_file() will return false.

● If file is a legitimate uploaded file but cannot be moved for some reason, no action will occur and move_uploaded_file() will return false and a warning will be issued.

This kind of check is particularly important if the uploaded file may cause its content to be displayed to the user or other users of this system.

php move_uploaded_file() function usage example

The following example creates an upload form that allows uploading files with a file size of less than 1MB. The specific sample code is as follows:

<html>
<body>
    <!--上传表单,有一个上传文件域-->
    <form method="post" action="" enctype="multipart/form-data" name="form">
        <input type="file" name="up_file">
        <!-- 提交按钮-->
      <input type="submit" name="submit" value="上传">
</form>
</body>
</html>
<?php
header("Content-Type:text/html; charset=utf-8");
if(!empty($_FILES[&#39;up_file&#39;][&#39;name&#39;])){       //判断是否有文件
     $fileinfo = $_FILES[&#39;up_file&#39;];      //将文件信息赋给变量$fileinfo
    if($fileinfo[&#39;size&#39;]<1000000 && $fileinfo[&#39;size&#39;]>0){    //判断文件大小
        move_uploaded_file($fileinfo[&#39;tmp_name&#39;],$fileinfo[&#39;name&#39;]);  //上传文件
        echo "上传成功";
    }else{
        echo &#39;文件太大或未知&#39;;
    }
}
?>
Copy after login

Select a file to upload, the output result is as follows:

How to use php move_uploaded_file function

Instructions:

Use the move_uploaded_file() function To upload a file, when creating a form, you must set the form form's enctype="multipart/form-data".

The above is the detailed content of How to use php move_uploaded_file function. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!