Rename uploaded file (php)

后端 未结 3 406
逝去的感伤
逝去的感伤 2020-12-22 13:27

I\'m trying to rename a file I\'m uploading.

I will be uploading a xml or pdf file, and I want it to be in a folder called \"files/orderid/\" and the filen

相关标签:
3条回答
  • 2020-12-22 13:47

    You set the target file to be the name of the file. You should be setting a different name.

    Try this:

    $target_dir = "files/$id/";
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $target_file = "{$target_dir}{$id}.{$imageFileType};
    
    0 讨论(0)
  • 2020-12-22 14:06

    Thanks guy, it all helped.

    My solution was to make a variable with the extension, and then use that through out the file.

    //Check file extension
    $path = $_FILES["fileToUpload"]["name"];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    

    and then

    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "files/$id/$id.$ext"
    
    0 讨论(0)
  • 2020-12-22 14:07

    Rename the file as below

    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $target_file = $target_dir . $id . '.' . $imageFileType;
    

    And then

    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    
    0 讨论(0)
提交回复
热议问题