我想要一个简单的指南,了解如何将具有活动状态的图像传输到另一个目录,这是我的代码:
Route::get('/', function () {
$path = public_path('images\products');
$allfiles = scandir($path);
$allfiles = array_diff(scandir($path), array('.', '..'));
$files = array();
foreach ($allfiles as $filename) {
$file= File::get(public_path('images/products/'.$filename));
$files[] = [
"file_name"=>$filename];
}
// return $filename;
foreach($files as $file){
$exists[] = DB::table('tw_products')
// ->where('image', '=', $file['file_name'])
->where('status', '=', 1)
->get('image', '=', $file['file_name']);
// return $exists;
if($exists[] = 'images/products/'.$file['file_name']){
File::copy('images/products/'.$file['file_name'],'images/ActiveProducts/'.$file['file_name']);
echo "File Moved from (images/products/".$file['file_name'] . ")<br />";
}
}
});
现在所有文件都被复制,但我不确定为什么它没有过滤图像 我想比较数据库项目(图像和状态),如果图像的状态= 1,该图像将被移动到另一个目录...
尝试使用 count 而不是 get ,它将根据文件是否处于活动状态返回 0 或 1
foreach($files as $file){
$exists = DB::table('tw_products')
->where('image', '=', $file['file_name'])
->where('status', '=', 1)
->count();
if($exists){
File::copy('images/products/'.$file['file_name'],'images/ActiveProducts/'.$file['file_name']);
echo "File Moved";
}
}
我建议您将其作为命令或修补程序来执行以避免超时