Home >Database >Mysql Tutorial >How to Rename Uploaded Files Before Saving Them to a Directory?

How to Rename Uploaded Files Before Saving Them to a Directory?

Susan Sarandon
Susan SarandonOriginal
2024-12-14 08:18:18189browse

How to Rename Uploaded Files Before Saving Them to a Directory?

Renaming Uploaded Files Before Saving Them to a Directory

In this context, the move_uploaded_file() function handles the preservation of uploaded files in a designated directory. To assign a random number as the file name, implement the following adjustments:

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

The modified code accomplishes the following:

  • $temp = explode(".", $_FILES"file"); separates the file extension from the file name.
  • $newfilename = round(microtime(true)) . '.' . end($temp); generates a random number based on the current time and appends the original file extension.
  • move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename); saves the uploaded file to the designated directory using the newly assigned random file name.

The above is the detailed content of How to Rename Uploaded Files Before Saving Them to a Directory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn