PHP file upload features allow you to upload binary and text files both. Moreover, you can have full control over the file to be uploaded through PHP authentication and file operation functions.
The PHP global $_FILES contains all the information of the file. With the help of $_FILES global, we can get the file name, file type, file size, temp file name and errors associated with the file.
Here, we are assuming that file name is filename.
The move_uploaded_file() function moves the uploaded file to a new location. The move_uploaded_file() function checks internally if the file is uploaded thorough the POST request. It moves the file if it is uploaded through the POST request.
The examples Below create a temporary copy of the uploaded files in the PHP temp folder on the web server and then PHP move_uploaded_file() function relocate uploaded file from temp directory to a our target destination.
The temporarily copied file auto-deleted when the script execution ends.
The examples above used some validations to ensure file upload as per the requirement.
bool move_uploaded_file ( string $filename , string $destination )
move_uploaded_file ("Your File Name With Temp Name","Your Directory Path With Image Name")
File Name: upload_form.html
<form action="file_uploader.php" method="post" enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>
</form>
File Name: file_ uploader.php
<?php
$target_path = "Your Directory Path";
$target_path = $target_path.basename( $_FILES["fileToUpload"]["name"]);
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_path)) {
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
}
?>
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning