【问题标题】:php is_file not detecting image with period in namephp is_file 未检测到名称中带有句点的图像
【发布时间】:2015-09-30 17:19:47
【问题描述】:

我有一个检查图像文件是否存在的功能。它适用于所有图像,除非文件名中有句点。文件名是用户上传的,许多已经存在但没有经过清理。这是一个例子:

$img = 'nice_name.jpg'; // detects
$img = 'bad_name.7.jpg'; // doesn't detect

if (is_file($path . $img)) {
    return $path . $prefix . $img;
}

我不知道如何摆脱它或让它发挥作用。我已经检查了两倍,并且该文件确实存在于该路径中。该功能适用​​于同一文件夹中的其他图像名称。

编辑:这被标记为重复并链接到有关上传文件的问题。我正在使用 is_file() 检查文件是否已经存在。没有上传,文件已经有额外的“。”在服务器上以它的名字命名,所以这是一个不同的问题。

【问题讨论】:

  • 我检查文件没有问题。在这种情况下,句点没有特殊含义。

标签: php


【解决方案1】:

您可以使用basename() 获取文件名,然后对它进行一些操作,例如如果它包含句点则重命名。

$testfile = "test.7.img";
$extension = ".img";
$filename = basename($testfile, $extension);
if(strpos($filename,".") > 0) {
    $newname = str_replace(".","",$filename) . $extension ;
    rename($testfile,$newname);
}
//... then continue on with your code

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 2011-10-17
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 2014-07-31
    • 2019-06-28
    相关资源
    最近更新 更多