迭代PHP文件数组并删除任何没有内容的文件
我有一个文件数组,每个文件都有完整的目录路径。我需要迭代我的文件数组,然后删除其中0字节/非内容的文件 files.txt迭代PHP文件数组并删除任何没有内容的文件,php,Php,我有一个文件数组,每个文件都有完整的目录路径。我需要迭代我的文件数组,然后删除其中0字节/非内容的文件 files.txt /lib/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php /lib/Zend/Gdata/App/Extension.php /lib/Zend/Gdata/App/MediaEntry.php /lib/Zend/Gdata/App/FeedEntryParent.php /lib/Zend/Gdata/App/Auth
/lib/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php
/lib/Zend/Gdata/App/Extension.php
/lib/Zend/Gdata/App/MediaEntry.php
/lib/Zend/Gdata/App/FeedEntryParent.php
/lib/Zend/Gdata/App/AuthException.php
/lib/Zend/ProgressBar/Adapter.php
/lib/Zend/ProgressBar/alias.php
/lib/Zend/Locale/code.php
/lib/Zend/Server/Reflection/Function/article.php
/lib/Zend/Server/Reflection/ReturnValue.php
/lib/Zend/Server/Reflection.php
/lib/Zend/Dojo/BuildLayer.php
/lib/Zend/Tag/Cloud/start.php
/lib/Zend/Tag/Cloud/user.php
/lib/Zend/Tag/Item.php
/lib/Zend/Tag/Cloud.php
/lib/Zend/Ldap/Filter/Not.php
/lib/Zend/Ldap/Filter/And.php
/lib/Zend/Ldap/Filter/Exception.php
/lib/Zend/Ldap/Node.php
/lib/Zend/Ldap/Exception.php
// list of files to download
$lines = file('files.txt');
// Loop through our array of files from the files.txt file
foreach ($lines as $line_num =>$file) {
echo htmlspecialchars($file) . "<br />\n";
// delete empty files
}
PHP
/lib/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php
/lib/Zend/Gdata/App/Extension.php
/lib/Zend/Gdata/App/MediaEntry.php
/lib/Zend/Gdata/App/FeedEntryParent.php
/lib/Zend/Gdata/App/AuthException.php
/lib/Zend/ProgressBar/Adapter.php
/lib/Zend/ProgressBar/alias.php
/lib/Zend/Locale/code.php
/lib/Zend/Server/Reflection/Function/article.php
/lib/Zend/Server/Reflection/ReturnValue.php
/lib/Zend/Server/Reflection.php
/lib/Zend/Dojo/BuildLayer.php
/lib/Zend/Tag/Cloud/start.php
/lib/Zend/Tag/Cloud/user.php
/lib/Zend/Tag/Item.php
/lib/Zend/Tag/Cloud.php
/lib/Zend/Ldap/Filter/Not.php
/lib/Zend/Ldap/Filter/And.php
/lib/Zend/Ldap/Filter/Exception.php
/lib/Zend/Ldap/Node.php
/lib/Zend/Ldap/Exception.php
// list of files to download
$lines = file('files.txt');
// Loop through our array of files from the files.txt file
foreach ($lines as $line_num =>$file) {
echo htmlspecialchars($file) . "<br />\n";
// delete empty files
}
//要下载的文件列表
$lines=file('files.txt');
//循环浏览files.txt文件中的文件数组
foreach($line作为$line\u num=>$file的行){
回显htmlspecialchars($file)。“
\n”;
//删除空文件
}
到目前为止,您的基本循环看起来不错,我认为您接下来感兴趣的是和:
$lines=file('files.txt',file\u IGNORE\u NEW\u行);
foreach($line作为$line\u num=>$file的行){
$file\u label=htmlspecialchars($file);
echo$file\u标签。“
\n”;
如果(!file_存在($file)){
回显“文件”。$file\u标签。“不存在
\n”;
}else if(文件大小($file)==0){
回显“删除文件:“.$file\u标签”。
\n;
取消链接($文件);
}
}
虽然您应该非常小心,以确保它只删除特定目录中的文件,但可能有一个永远不应该删除的文件的白名单,等等
更新注释中的一个好注意事项是使用
文件()中的文件\u IGNORE\u NEW\u行
调用来剥离\r
和\n
返回的每行字符=]有两个函数可以执行此操作一个是文件大小()
检查文件大小,另一个是文件\u exists()
检查文件是否存在。要删除文件,请使用unlink()
函数
foreach ($lines as $line_num =>$file) {
if(file_exists($file) && filesize($file) === 0) {
unlink($file);
}
}
问题是?检查@Robert当我访问文本文件中的每个文件时,我有该文件的文件路径,但我也需要帮助检查它是否为空文件,如果为空,则我需要删除该文件。如果不为空,则忽略并检查下一个file@JasonDavis请更新您的问题小提示:OP可能想在FILE()
调用中添加标志:FILE\u IGNORE\u NEW\u line
,否则他将获得文件大小:xy.php\n
这在任何情况下都不起作用filesize()
可以返回FALSE,并且您不使用=
运算符。false==0
为真。@JasonDavis不客气。我已经认为这会引起一个问题:]看起来不错。大小检查是我需要帮助的,所以我尝试了echo$file.':'。文件大小($file)。'字节
但是,它基于如下路径为每个文件返回空值/lib/Zend/Tag/Cloud/start.php
任何ideasit都可能不是空的,而是false,这是不同的。如果读取文件时出现错误,例如权限,则filesize()将返回false。您还需要纠正路径是否正确,这可以通过文件来完成。\u exists()问题是文件中缺少文件\u IGNORE\u新行标志()