当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP move_uploaded_file()用法及代码示例


move_uploaded_file()function 是 PHP 中的内置函数,用于更改文件目标。该函数仅在通过 PHP POST 函数上传文件时起作用。如果文件有效,则会上传。

用法:

move_uploaded_file( string $from, string $to ): bool

Parameters: 该函数接受两个参数,如下所述。

  • $from: 该参数指定文件的临时位置。这是临时存储您上传的文件的位置。
  • $to 此参数指定文件存储位置的目标。

返回值:如果函数成功更改文件位置,则此函数返回 true,否则此函数将返回 false。

程序1:下面的程序演示了move_uploaded_file()函数。

PHP


/* upload.php */
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (
        isset($_FILES["file_upload"]) &&
        $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK
    ) {
          // Add name of upload directory
        $uploadDirectory = "./uploads/";
        if (move_uploaded_file(
            $_FILES["file_upload"]["tmp_name"],
            $uploadDirectory . $_FILES["file_upload"]["name"]
            )) {
            echo "File uploaded successfully!";
        } else {
            echo "Error moving file.";
        }
    } else {
        echo "Error uploading file.";
    }
}
?>

HTML


<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Simple File Upload</title>
</head>
<body>
    <form action="upload.php" method="post"
        enctype="multipart/form-data">
         
        <label for="file_upload">
            Choose a file:
        </label>
         
        <input type="file" name="file_upload"
            id="file_upload">
         
        <input type="submit" value="Upload File">
    </form>
</body>
</html>

输出:

uploaded

程序2:下面的程序演示了move_uploaded_file()函数。在此示例中,我们仅上传特定扩展。

PHP


/* upload.php */
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (
        isset($_FILES["file_upload"]) &&
        $_FILES["file_upload"]["error"] == UPLOAD_ERR_OK
    ) {
        $uploadDirectory = "uploads/";
        $originalFileName = 
            basename($_FILES["file_upload"]["name"]);
        $fileExtension = pathinfo(
            $originalFileName, PATHINFO_EXTENSION);
        $allowedExtensions = ["jpg", "jpeg", "png"];
        if (in_array($fileExtension, $allowedExtensions)) {
            if (
                move_uploaded_file(
                    $_FILES["file_upload"]["tmp_name"],
                    $uploadDirectory . $originalFileName
                )
            ) {
                echo "File uploaded successfully!";
            } else {
                echo "Error moving file.";
            }
        } else {
            echo "Error: Only JPG, JPEG, and "
                . "PNG files are allowed.";
        }
    } else {
        echo "Error uploading file.";
    }
}
?>

HTML


<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>File Upload Example</title>
</head>
<body>
    <form action="upload.php" method="post"
        enctype="multipart/form-data">
        <label for="file_upload">
            Choose a file (only JPG, JPEG, and PNG):
        </label>
         
        <input type="file" name="file_upload"
            id="file_upload" accept=".jpg, .jpeg, .png">
         
        <input type="submit" value="Upload File">
    </form>
</body>
</html>

输出:

uploaded参考: https://www.php.net/manual/en/function.move-uploaded-file.php



相关用法


注:本文由纯净天空筛选整理自neeraj3304大神的英文原创作品 PHP move_uploaded_file() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。