dongquweng5152 2015-10-26 05:11
浏览 80
已采纳

上传时更改文件名

I currently have a form with 2x name=userfile[] attributes in the inputs that is handled within the code below. What would be the best way to enable me to rename the filenames foreach file on upload - I want them to be specific to the input

What I am after:

$imageOneName = img1.$var;
$imageTwoName = img2.$var;

Code:

for($i=0; $i<count($_FILES['userfile']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['userfile']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = $local_path .'images/' . $_FILES['userfile']['name'][$i];


    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}
  • 写回答

3条回答 默认 最新

  • duancheng6500 2015-10-26 05:24
    关注

    Instead of

    <input type="file" name="userfile[]" id="input1">
    <input type="file" name="userfile[]" id="input2">
    

    You can do the following to distinguish between the two

    <input type="file" name="userfile[desiredNameOfFile1]" id="input1">
    <input type="file" name="userfile[desiredNameOfFile2]" id="input2">
    

    With PHP handling it like this:

    foreach($_FILES['userFile']['name'] AS $desiredNameOfFile => $fileInfo) {
      //Get the temp file path
      $tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile];
    
      //Make sure we have a filepath
      if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = $local_path .'images/' . $desiredNameOfFile . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile],PATHINFO_EXTENSION);
    
    
        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
          //Handle other code here
    
        }
      }
    }
    

    Be aware: this code will overwrite files that already have that name

    Edit

    If you want multiple file selects

    <input type="file" name="userfile[desiredNameOfFile1][]" id="input1" multiple>
    <input type="file" name="userfile[desiredNameOfFile2][]" id="input2" multiple>
    

    Php

    foreach($_FILES['userfile']['name'] AS $desiredNameOfFile => $fileInfo) {
        for($i = 0; $i < count($fileInfo); $i++) {
        //Get the temp file path
            $tmpFilePath = $_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i];
    
            // Make sure we have a filepath
            if ($tmpFilePath != ""){
                // Setup our new file path
                $newFilePath = $local_path .'images/' . $desiredNameOfFile . $i . pathInfo($_FILES['userfile']['tmp_name'][$desiredNameOfFile][$i],PATHINFO_EXTENSION);
    
    
                // Upload the file into the temp dir
                if(move_uploaded_file($tmpFilePath, $newFilePath)) {
    
                    // Handle other code here
    
                }
            }
        }
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料