Moves an uploaded file.
This function moves an uploaded file to the specified location. For security reasons, it is a good idea to always use this function on uploaded files. move_uploaded_file() checks that the file is an uploaded file before moving it. If the file was successfully moved, TRUE is returned. Otherwise FALSE is returned.
<?php
if (!isset($_REQUEST["seenform"])) {
?>
<form enctype="multipart/form-data" action="move_uploaded_file.php" method="post">
Upload file: <input name="userfile" type="file">
<input type="submit" value="Upload">
<input type="hidden" name="seenform">
</form>
<?php
} else {
$uploaded_dir = "./upload/";
$filename = $_FILES["userfile"]["name"];
$path = $uploaded_dir . $filename;
print "Temporary name: " . $_FILES['userfile']['tmp_name'] . "<br>";
print "Original name: $filename<br>";
print "Destination: $path<br>";
if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $path)) {
print "Uploaded file moved";
// do something with the file here
} else {
print "Move failed";
}
}
?> Temporary name: /var/tmp/php746.tmp
Original name: code.php
Destination: ./upload/code.php
Uploaded file moved The first time the page is loaded, a form is shown where the user can upload a file. When the button is pressed, the file is posted back to the same page. move_uploaded_file() is used to move the file to the upload directory.