如何检查仅文件名而不存在扩展名


How can I check only file name not extension is exists

如何只检查文件名而不是像jpeg, jpg, doc, xls那样的扩展名,然后复制其完整名称,如example.jpegexample.doc

如果可能的话,我们可以存储它的上两个父目录,如

如果示例.jpeg存储在

main_dir/second_dir/example.jpeg 

所以我想将此路径存储在 php 变量中。

我知道我可以使用glob()

$result = glob ("./uploads/filename.*");

并检查$result值。

但是我想存储完整的文件名以及可能的两个父目录路径。

下面是我的代码

$filename = '/www/test1/'. $file . '*' ;
if (count(glob($filename)) > 0) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

编辑

根据 luweiqi 解决方案更新的查询

foreach(glob("/uploads/".$productnumber."/". $productnumber."b.*", GLOB_NOSORT) as $file) {  
        echo "Filename: " . $file . "<br />";      
        $image2 = "/uploads/".$productnumber."/".$file;
    }  

图像的最后一个字母像 a 到 f 一样变化。 所以你可以对它进行一些更正。我想检查图像是否存在于上传/产品编号/产品编号 (a/b/c/d/e/f).jpg或 png 或 jpeg 等,并将该文件名存储在 php 变量中。

您可以使用 file_exists() 函数。

$pngFile = '/path/to/foo.png';
$docFile = '/path/to/foo.doc';
// Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
if (file_exists($filename) || file_exists($docFile)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

使用球功能

$files=[];
$result = glob ("/path/to/foo.*");  
foreach ($result as $file) { 
     echo "$file size " . filesize($file) . "'n";       
     $files[] = $file; 
} 
echo '<pre>'; print_r($files);

您可以使用:

<?php
    foreach(glob("../directory/*{a,b,c,e,f}.*", GLOB_BRACE) as $file) {
        echo "Filename: " . $file . "<br />";
    }
?>

此代码将获取文件名并回显它,如果要将其分配给变量,可以相应地更改它。