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

前端 未结 5 949
清酒与你
清酒与你 2020-11-22 03:20

Below is the code I used in order to upload files into a directory. It works fine. My main question is:

move_uploaded_file() is the one that saves t

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 04:13

    You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file.

    Instead of

    move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);
    

    Use

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

    Changed to reflect your question, will product a random number based on the current time and append the extension from the originally uploaded file.

提交回复
热议问题