Horje
PHP move_uploaded_file() Function

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

/* 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




Reffered: https://www.geeksforgeeks.org


PHP

Related
Difference Between Core PHP and CakePHP Difference Between Core PHP and CakePHP
PHP String Programs PHP String Programs
PHP Array Programs PHP Array Programs
PHP hebrev() Function PHP hebrev() Function
PHP SplFileObject fflush() Function PHP SplFileObject fflush() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15