附加到PHP变量,但在';之前;。jpg';/';。巴布亚新几内亚';等
所以我创建了一个上传图像的基本代码。用户上传2个图像,当它们被处理/上传时,我有一点代码来确保它们上传到服务器时文件名不相同附加到PHP变量,但在';之前;。jpg';/';。巴布亚新几内亚';等,php,append,Php,Append,所以我创建了一个上传图像的基本代码。用户上传2个图像,当它们被处理/上传时,我有一点代码来确保它们上传到服务器时文件名不相同 if(file_exists($imgpath1)){ $imgpath1 = $imgpath1 . $random; } if(file_exists($imgpath2)){ $imgpath2 = $imgpath2 . $random; } 让我们以“$imgpath1=”images/user/1.jpg”开始(在运行上述PHP之前) $ra
if(file_exists($imgpath1)){
$imgpath1 = $imgpath1 . $random;
}
if(file_exists($imgpath2)){
$imgpath2 = $imgpath2 . $random;
}
让我们以“$imgpath1=”images/user/1.jpg”开始(在运行上述PHP之前)
$random是脚本开始时生成的随机数,比如说$random='255'
代码工作正常,图像仍能正确显示,但它将“255”($random)直接添加到文件路径的末尾,因此在上述代码运行后,$imgpath1=“images/user/1.jpg255”
文件扩展名并不总是.jpg。显然,它可以是.png、.bmp等等
如何使$random(本例中为255)位于文件路径中的“.jpg”之前?我曾尝试在谷歌上搜索,但我似乎无法用正确的词来找到任何有用的答案
谢谢您可以使用
pathinfo
功能获取所需的APRT,并使用添加的随机零件重建:
if(file_exists($imgpath1)){
$pathinfo = pathinfo($imgpath1);
$imgpath1 = $pathinfo['dirname'] .'/' . $pathinfo['filename'] . $random . '.' . $pathinfo['extension'];
}
尽管您的$random
变量需要是唯一的id,否则仍然会发生冲突
您还需要过滤掉坏字符(服务器上不同文件系统的用户等)。通常,用
uniqid()替换全名比较容易$路径信息['extension']代码>您可以尝试以下代码:
$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
(Ctrl+ENTER进行测试)您可以这样做:
这将提取最后一个时段($regs[1])之前的路径和文件名,以及字符串($regs[2])结束之前的其他路径和文件名
使用文件名,如/path/subpath/filename.jpg或/path/subpath/filename.saved.jpg等
Regex的意思是:
# ^(.*)\.([^.].*)$
#
# 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) «$»
在
上分解,获取分解数组的最后一个元素并取消设置数组中的最后一个元素(数组\u pop
做得很好),使用
内爆所有剩余元素,然后将所有三个部分放在一起。:-)太好了,谢谢!那是我完成了lmao当天的工作
# ^(.*)\.([^.].*)$
#
# 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) «$»