How to upload file in PHP?

Last updated 5 years ago | 1218 views 75     5

Tags:- PHP

PHP | Upload File

By using move_uploaded_file() function we can upload file in PHP.The move_uploaded_file() function moves an uploaded file to a new location.

This function returns TRUE on success or FALSE on failure.

move_uploaded_file(file,newloc)
 
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
?>