All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Move Uploaded File In PHP

Last Updated : Jul 1, 2023

Move Uploaded File In PHP

In this tutorial we will show you the solution of move uploaded file in PHP, it's critical to move the uploaded file to its destination directory via the backend.

To transfer an uploaded file, PHP provides the move uploaded file() method by default.

We'll check some of the most typical concerns with move uploaded file in this lesson.

And perhaps, when you use this function, it will make your life easier. Furthermore, it may assist you in resolving some of your current bugs.

Step By Step Guide On Move Uploaded File In PHP :-

The move_uploaded_file method verifies that the file specified by from is a valid upload file (i.e., one that was created using PHP's HTTP POST upload protocol).

The file will be relocated to the filename given by to if it is valid.

Parameter

file_path (mandatory): provide the file's new location

destination (mandatory): specifies the uploaded file's new location

Return Value

  • move uploaded file returns a boolean value, ie:
  • Returns TRUE if the operation was successful.
  • If the file isn't a valid upload, it returns FALSE.
  • If the file is valid to upload but cannot be moved owing to any difficulties, returns FALSE with a warning message.

HTML File Code

<!DOCTYPE html>
<head>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file">
        <button type="submit" name="submit">UPLOAD FILE</button>
    </form>
</body>
</html>
  1. This function may verify that the file specified by filename is a valid upload file, meaning it has been uploaded via PHP's HTTP POST upload protocol.
  2. If the file is genuine, it can be relocated to the destination's filename.
  3. This type of check is particularly useful if anything done with uploaded files has the potential to reveal their contents to the users, or even to other people on the same system.

PHP File

<?php
if(isset($_POST['submit'])){
    $file = $_FILES['file'];
    $fileName = $_FILES['file']['name'];
    $fileTempName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
    $fileExt = explode('.',$fileName);
    $fileActualExt = strtolower(end($fileExt));
    $allowedExt = array("jpg","jpeg","png","pdf");
    if(in_array($fileActualExt, $allowedExt)){
        if($fileError == 0){
            if($fileSize < 10000000){
                $fileNemeNew = uniqid('',true).".".$fileActualExt;
                $fileDestination = 'Uploads/'.$fileNemeNew;
                move_uploaded_file($fileTempName, $fileDestination);
                echo "File Uploaded successfully";
            }else{
                echo "File Size Limit beyond acceptance";
            }
        }else{
            echo "Something Went Wrong Please try again!";
        }
    }else{
        echo "You can not upload this extention of file";
    }
}
?>
  1. This function first checks if the uploaded file is a valid upload before relocating it to a new location (i.e. uploaded using HTTP POST mechanism).
  2. As a result, we can argue that this function is only useful if the file is submitted using the HTTP POST method.
  3. The destination file will be overwritten if it already exists.
  4. This function is compatible with PHP versions 4.0.3 and above.

Conclusion :-

Thank you for taking the time to read this post; if you have any questions, have an alternative answer, or believe our solution is incorrect, please let us know in the comments area.

Use of move_uploaded_file in PHP projects is unavoidable if you're a PHP coder. To manage file *uploading*, move_uploaded_file is utilised.

Although it appears to be a very straightfoward process, many developers have struggled with it. I hope this tutorial on move uploaded file in PHP helps you.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪