How To Use PHP move_uploaded_file To Upload File In Specific Location
Hello, Are you finding PHP code to upload a file in the specific location? Unable to move your file to the specific folder using PHP? Don’t worry. We are here to give you best solution to move your file into the specific folder. You will get the direct code to upload file in PHP. We are here posting PHP move_uploaded_file script for you. So, follow our tutorial and upload your file to the specific folder easily.
PHP move_uploaded_file Function
The move_uploaded_file() function is used to move your uploaded file to the specific location. The move_uploaded_file() function returns true if file successfully moved and false if it fails to move. The PHP move_uploaded_file will overwrite the file if the file already exists with the same name.
PHP File Upload Script
<title>PHP move_uploaded_file To Upload File In Specific Location</title>
<form action="" method="post" enctype="multipart/form-data">
<label>Choose a file to be uploaded</label>
<input type="file" name="file"/>
<input type="submit" name="upload" value="Upload"/>
$targetfolder = "uploaded_file/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
echo "The file ". basename( $_FILES['file']['name']). " is uploaded successfully.";
echo "Problem uploading file.";
In the above code, We have taken one form. In that form, We have taken one input type=”file” to upload the file and one submit button. In the PHP code, We have taken move_uploaded_file and basename functions to move selected file in the desired location. You should make one folder and add the name of that folder to $targetfolder to move your uploaded file in that. We have made uploaded_file folder and added that in the code.
PHP File Upload Error
PHP file upload error occurs when you forgot to select the file and press upload button. But in most of the case, user forgot to add enctype=”multipart/form-data” and an error occurs. So, you must add enctype=”multipart/form-data” to move your file to the specified location.
PHP File Upload Success
If your file uploaded successfully to the desired targeted folder then you will get the success message like given in above screenshot. That’s it. You can check the targeted folder, You will get your uploaded file there.