附加到 PHP 变量,但在 '.jpg' / '.png' 等之前
Append to PHP variable, but before '.jpg' / '.png' etc
所以我创建了一个上传图片的基本代码。用户上传了 2 张图片,当它们被上传时 processed/uploaded 我有一小段代码来确保文件名在上传到服务器时不相同
if(file_exists($imgpath1)){
$imgpath1 = $imgpath1 . $random;
}
if(file_exists($imgpath2)){
$imgpath2 = $imgpath2 . $random;
}
假设 $imgpath1 = "images/user/1.jpg" 开始(在 PHP以上是运行)
和$运行dom是在脚本开头生成的运行dom编号,比方说$运行dom = '255'.
代码运行完美,图像仍然正确显示,但它是将“255”($运行dom)直接添加到文件路径的末尾,所以 $imgpath1 = "images/user/1.jpg255" 上面的代码后有 运行.
文件扩展名显然不会总是.jpg,它可以是.png、.bmp 等等...
如何使 $运行dom(在本例中为 255)位于文件路径中的“.jpg”之前?我曾尝试研究 google,但我似乎无法正确表达它以找到任何有用的答案。
谢谢
您可以使用pathinfo
函数获取所需的aprts,并添加随机部分重建:
if(file_exists($imgpath1)){
$pathinfo = pathinfo($imgpath1);
$imgpath1 = $pathinfo['dirname'] .'/' . $pathinfo['filename'] . $random . '.' . $pathinfo['extension'];
}
尽管您的 $random
变量需要是唯一 ID,否则您仍然会发生冲突。
您还需要过滤掉不良字符(与您的服务器在不同文件系统上的人等)。通常用 uniqid() . '.' . $pathinfo['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
http://www.tehplayground.com/#JFiiRpjBX(Ctrl+ENTER 进行测试)
你可以这样做:
这将提取 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) «$»
所以我创建了一个上传图片的基本代码。用户上传了 2 张图片,当它们被上传时 processed/uploaded 我有一小段代码来确保文件名在上传到服务器时不相同
if(file_exists($imgpath1)){
$imgpath1 = $imgpath1 . $random;
}
if(file_exists($imgpath2)){
$imgpath2 = $imgpath2 . $random;
}
假设 $imgpath1 = "images/user/1.jpg" 开始(在 PHP以上是运行)
和$运行dom是在脚本开头生成的运行dom编号,比方说$运行dom = '255'.
代码运行完美,图像仍然正确显示,但它是将“255”($运行dom)直接添加到文件路径的末尾,所以 $imgpath1 = "images/user/1.jpg255" 上面的代码后有 运行.
文件扩展名显然不会总是.jpg,它可以是.png、.bmp 等等...
如何使 $运行dom(在本例中为 255)位于文件路径中的“.jpg”之前?我曾尝试研究 google,但我似乎无法正确表达它以找到任何有用的答案。
谢谢
您可以使用pathinfo
函数获取所需的aprts,并添加随机部分重建:
if(file_exists($imgpath1)){
$pathinfo = pathinfo($imgpath1);
$imgpath1 = $pathinfo['dirname'] .'/' . $pathinfo['filename'] . $random . '.' . $pathinfo['extension'];
}
尽管您的 $random
变量需要是唯一 ID,否则您仍然会发生冲突。
您还需要过滤掉不良字符(与您的服务器在不同文件系统上的人等)。通常用 uniqid() . '.' . $pathinfo['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
http://www.tehplayground.com/#JFiiRpjBX(Ctrl+ENTER 进行测试)
你可以这样做:
这将提取 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) «$»