Open In App

PHP move_uploaded_file() Function

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The move_uploaded_file() function is an inbuilt function in PHP that is used to change the file destination. This function only works when the file is uploaded by the PHP POST function. If the file is valid it will uploaded.

Syntax:

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

Parameters: This function accepts two parameters that are described below.

  • $from: This parameter specifies the temporary location of the file. This is the location where your uploaded file is stored temporarily.
  • $to: This parameter specifies the destination of the location where your file will be stored.

Return Value: This function returns true if the function successfully changes the location of the file otherwise this function will return false.

Program 1: The following program demonstrates the move_uploaded_file() function.

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>

Output:

uploaded

Program 2: The following program demonstrates the move_uploaded_file() function. In this example we are uploading only specific extensions.

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>

Output:

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


Next Article

Similar Reads