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

前端 未结 5 888
清酒与你
清酒与你 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:12

    /* create new name file */
    $filename   = uniqid() . "-" . time(); // 5dab1961e93a7-1571494241
    $extension  = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION ); // jpg
    $basename   = $filename . "." . $extension; // 5dab1961e93a7_1571494241.jpg
    
    $source       = $_FILES["file"]["tmp_name"];
    $destination  = "../img/imageDirectory/{$basename}";
    
    /* move the file */
    move_uploaded_file( $source, $destination );
    
    echo "Stored in: {$destination}";
    

提交回复
热议问题