PHP中Linux文件路径是否存在怎么判断?

2023-03-17 18:29 323 阅读量

php linux文件路径是否存在的判断方法:1、使用linux命令“[ -f qipa250.txt ] && echo yes || echo no”判断文件是否存在;2、通过php调用linux命令,代码是“$pdf_file_exists = '[ -f ' . $pdf_file_url . ' ] && echo true || echo false';”

 

本教程操作环境:linux5.9.8系统、PHP8.1、Dell G3电脑。

php使用linux命令判断文件是否存在

Linux一句命令之判断文件是否存在

[ -f qipa250.txt ] && echo yes || echo no

-f 文件名字文件存在则为真。

执行[ -f qipa250.txt ]为真则执行echo yes,由于或语句||的存在echo no不再执行。

特别注意的是,这里的逻辑与和逻辑或值得仔细思考。

php调用linux命令方法

//指定文件路径

$pdf_file_url='/data/web/QipaFile/qipa250.pdf';

//命令

$pdf_file_exists = '[ -f ' . $pdf_file_url . ' ] && echo true || echo false';

//执行

echo $pdf_file_exists_result = system($pdf_file_exists);