How to rename the uploaded file before saving it into a directory with PHP?

Spread the love

Sometimes, we want to rename the uploaded file before saving it into a directory with PHP.

In this article, we’ll look at how to rename the uploaded file before saving it into a directory with PHP.

How to rename the uploaded file before saving it into a directory with PHP?

To rename the uploaded file before saving it into a directory with PHP, we can use the move_uploaded_file function.

For instance, we write

$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . "." . end($temp);
move_uploaded_file(
    $_FILES["file"]["tmp_name"],
    "../img/imageDirectory/" . $newfilename
);

to call move_uploaded_file with the temporary file name of the uploaded file $_FILES["file"]["tmp_name"] and the path we want to move the file to respectively.

Conclusion

To rename the uploaded file before saving it into a directory with PHP, we can use the move_uploaded_file function.

Leave a Reply

Your email address will not be published.