【问题标题】:Append to PHP variable, but before '.jpg' / '.png' etc附加到 PHP 变量,但在 '.jpg' / '.png' 等之前
【发布时间】:2016-05-03 07:23:06
【问题描述】:

所以我创建了一个上传图片的基本代码。用户上传了 2 张图片,当它们被处理/上传时,我有一些代码来确保文件名在上传到服务器时不一样

if(file_exists($imgpath1)){
    $imgpath1 = $imgpath1 . $random;
}
if(file_exists($imgpath2)){
    $imgpath2 = $imgpath2 . $random;
}

假设以 $imgpath1 = "images/user/1.jpg" 开头(在运行上述 PHP 之前)

$random 是在脚本开始时生成的随机数,比如 $random = '255'。 p>

代码完美运行,图像仍然正确显示,但它直接将 '255' ($random) 添加到文件路径的末尾,所以 $imgpath1 = "images/user /1.jpg255" 在上面的代码运行之后。

文件扩展名显然不会总是.jpg,它可能是.png、.bmp等等......

如何使 $random(在本例中为 255)在文件路径中的“.jpg”之前出现?我曾尝试在谷歌上进行研究,但我似乎无法正确地用词来找到任何有用的答案。

谢谢

【问题讨论】:

  • . 上分解,获取分解数组的最后一个元素并取消设置数组中的最后一个元素(array_pop 做得很好),用. 内爆所有剩余元素,然后将所有三个部分一起。 :-)

标签: php append


【解决方案1】:

您可以使用pathinfo 函数获取所需的aprts,并使用添加的随机部分进行重建:

if(file_exists($imgpath1)){
    $pathinfo = pathinfo($imgpath1);
    $imgpath1 = $pathinfo['dirname'] .'/' . $pathinfo['filename'] . $random . '.' . $pathinfo['extension'];
}

尽管您的 $random 变量需要是唯一的 id,否则您仍然会发生冲突。

您还需要过滤掉坏字符(不同文件系统上的人到您的服务器等)。通常用uniqid() . '.' . $pathinfo['extension']; 替换整个名称更容易

【讨论】:

    【解决方案2】:

    你可以试试这个代码:

    $filename = "file.jpg";
    $append = "001";
    
    function append_filename($filename, $append) {
        preg_match ("#^(.*)\.(.+?)$#", $filename , $matches);
        return $matches[1].$append.'.'.$matches[2];
    }
    
    echo append_filename($filename, $append);
    

    它给出:file001.jpg

    http://www.tehplayground.com/#JFiiRpjBX(Ctrl+ENTER 测试)

    【讨论】:

    • 完美,谢谢!这就是我今天完成的工作 lmao ?
    【解决方案3】:

    你可以这样做:

    这将提取 last 句点 ($regs[1]) 之前的路径和文件名,其余部分直到字符串结尾 ($regs[2])。

    if (preg_match('/^(.*)\.([^.].*)$/i', $imgpath1, $regs)) {
        $myfilename =  $regs[1] . $random . $regs[2];
    } else 
       $myfilename =  $imgpath1;
    }
    

    适用于文件文件名,如 /path/subpath/filename.jpg/path/subpath/filename.saved.jpg 等。

    正则表达式的含义:

    # ^(.*)\.([^.].*)$
    # 
    # Assert position at the beginning of the string «^»
    # Match the regular expression below and capture its match into     backreference number 1 «(.*)»
    #    Match any single character that is not a line break character «.*»
    #       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    # Match the character “.” literally «\.»
    # Match the regular expression below and capture its match into backreference number 2 «([^.].*)»
    #    Match any character that is NOT a “.” «[^.]»
    #    Match any single character that is not a line break character «.*»
    #       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    # Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2016-07-14
      • 1970-01-01
      • 2012-12-07
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多