文件扩展名中的PHP跳过区分大小写
我的数据库表中有一些行在目录中没有同步 我的桌子上有文件扩展名中的PHP跳过区分大小写,php,file-exists,Php,File Exists,我的数据库表中有一些行在目录中没有同步 我的桌子上有 image.png 但是在我的目录上我有 image.PNG 现在,我有个问题要检查一下 file_exist 因为区分大小写。 我无法选择手动同步我的数据库和目录上的文件的选项,因为它太多了 我可以知道如何使用忽略文件类型敏感度的文件exist吗?创建一个检查两个扩展名的函数,并使用它而不是直接使用文件exist。有关更通用的解决方案,请参阅php文档中的注释: 它提供此版本的功能: /** * Alternative to fil
image.png
但是在我的目录上我有
image.PNG
现在,我有个问题要检查一下
file_exist
因为区分大小写。
我无法选择手动同步我的数据库和目录上的文件的选项,因为它太多了
我可以知道如何使用忽略文件类型敏感度的文件exist吗?创建一个检查两个扩展名的函数,并使用它而不是直接使用文件exist。有关更通用的解决方案,请参阅php文档中的注释: 它提供此版本的功能:
/**
* Alternative to file_exists() that will also return true if a file exists
* with the same name in a different case.
* eg. say there exists a file /path/product.class.php
* file_exists('/path/Product.class.php')
* => false
* similar_file_exists('/path/Product.class.php')
* => true
*/
function similar_file_exists($filename) {
if (file_exists($filename)) {
return true;
}
$dir = dirname($filename);
$files = glob($dir . '/*');
$lcaseFilename = strtolower($filename);
foreach($files as $file) {
if (strtolower($file) == $lcaseFilename) {
return true;
}
}
return false;
}
我认为,你的根本原因在于- 如果数据库中有image.png和in directory image.png,则表示插入查询有问题。
这两个地方必须是一样的。最好建议以小写形式存储文件名,因为Linux系统是区分大小写的。可能重复的u可以轻松地以小写形式存储文件或使用正确的名称将文件放入数据库中。当然,你可以每次运行throw目录来查找真实的名称,但它的解决方案是错误的,如果你抛出数千个文件,这会使php进程进入磁盘等待状态并丢弃请求。