Php 如何检查仅存在文件名而不存在扩展名
如何仅检查文件名而不检查扩展名,如Php 如何检查仅存在文件名而不存在扩展名,php,Php,如何仅检查文件名而不检查扩展名,如jpeg、jpg、doc、xls。然后复制其完整名称,如example.jpeg或example.doc 如果可能的话,我们可以像 如果example.jpeg存储在 main_dir/second_dir/example.jpeg 所以我想把这个路径存储在php变量中 我知道我可以使用glob() 并检查$result值 但我想存储完整的文件名和可能的两个父目录路径 下面是我的代码 $filename = '/www/test1/'. $file . '*
jpeg、jpg、doc、xls
。然后复制其完整名称,如example.jpeg
或example.doc
如果可能的话,我们可以像
如果example.jpeg存储在
main_dir/second_dir/example.jpeg
所以我想把这个路径存储在php变量中
我知道我可以使用glob()
并检查$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;
}
foreach(glob(“/uploads/”$productnumber.”/“$productnumber.”b.*”,glob_NOSORT)作为$file){
回显“文件名:.”文件名“
”;
$image2=“/uploads/”$productnumber./”$file;
}
图像的最后一个字母变化类似于a到f。所以你能对它做些修正吗。
我想检查上传的图像是否存在/productnumber/productnumber(a/b/c/d/e/f).jpg或png或jpeg等,并将该文件名存储在php变量中 您可以使用函数
$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作为$file){
回显“$file size”.filesize($file)。“\n”;
$files[]=$file;
}
回声';打印(文件);
您可以使用:
这段代码将获得文件名并回显它,如果您想将其分配给变量,您可以相应地更改它。真正的问题是:?嗨,鸣人,正在等待这个问题。请检查更新问题。让我试试这个。。我可以使用php变量在路径中使用适当的连接,如。我的目录结构基于product_no.@John我不理解您的上下文,也许可以在您的问题中详细说明您想要实现的目标,谢谢您(glob(“/uploads/”$productnumber.”/“$productnumber.”b.*”,glob_NOSORT)作为$file){echo“Filename:“.file.”
“;$image2=“/uploads/”$productnumber.”.$file;}图像的最后一个字母的变化类似于a到f。所以你能对它做些修正吗。我想检查上传的图片是否存在/productnumber/productnumber(lastletter,如bc.jpg/png等),并将该文件名存储在php变量中。嗨,拉维,我已经更新了我的问题。你能检查一下吗。
$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 />";
}
?>