php - 发送 PHPMailer 电子邮件后删除“附件”文件夹
问题描述
我有一个 Web 表单,其中每个用户都有一个唯一的上传文件夹(使用他们的 PHP session_id() 作为文件夹名称),效果很好。提交表单时(经过错误检查)PHPMailer 用于发送电子邮件和附件。这也运作良好。但是,在发送电子邮件后,我想从文件夹中删除上传内容,然后是文件夹本身(有点像自我清理!)文件按预期删除,但文件夹仍然存在(尽管是空的)。我想知道该文件夹是否以某种方式“仍在使用”所以不会被删除或类似的东西?这是代码:
// Empty the contents of the upload folder
if (is_dir($dir)) { // Target directory ($dir) is set above in photos POST section
// Check for any files inside the directory
$files = glob($dir.'/*'); // Get all file names
foreach($files as $file) { // Iterate through the files
if(is_file($file)) { // Check its a file
unlink($file); // Delete the file
}
}
// Remove the upload folder
rmdir($dir); //NOT WORKING? NEEDS SOME TROUBLESHOOTING...
}
关于为什么保留此文件夹的任何其他想法?
本
解决方案
我猜你的文件夹可能包含默认 glob 模式不匹配的.
隐藏文件(以 开头),所以试试这个:
$files = glob($dir . '/{,.}*'); // Get all file names including hidden ones
foreach($files as $file) { // Iterate through the files
if(is_file($file)) { // Check its a file
unlink($file); // Delete the file
}
}
还要检查 unlink 和 rmdir 的返回值,这样你就可以准确地看到它失败的地方。
推荐阅读
- java - 在hibernate中使用SINGLE_TABLE继承策略时如何解决缩小代理警告?
- python - 将 Instagram 响应设备时间戳转换为可读日期时间
- xcode - MacOS 看不到命令行开发者工具
- node.js - 为什么在 mongodb 中尝试获取数组的长度时出现此错误?
- postman - 邮递员中的错误,即无法得到任何响应
- r - 当打开 RI 出现错误:'\r' is an unrecognized escape in string started ""c:\r" 我该如何解决
- python - 如何预处理具有太多 NAN 值的列?
- java - java中的百分比函数
- python - 图像处理 - 如何堆叠一个通道图像以创建一个 2 通道阵列
- python - 在python中的rdp(Ramer-Douglas-Peucker)算法中查找丢弃的点