如果图像的状态为1(活动),我想将图像移动到另一个目录,我可以移动一条记录,但不能移动全部记录

问题描述 投票:0回答:1

我想要一个简单的指南,了解如何将具有活动状态的图像传输到另一个目录,这是我的代码:

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,该图像将被移动到另一个目录...

php laravel laravel-5 eloquent laravel-8
1个回答
0
投票

尝试使用 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";
    }
}

我建议您将其作为命令或修补程序来执行以避免超时

© www.soinside.com 2019 - 2024. All rights reserved.