dongqiao8502 2018-01-22 01:06
浏览 113
已采纳

foreach只保存第一个数组,即使print_r正确打印出错误的结果

I am building a function to download images from an external source, and then write the id of each product has been downloaded along with the path to json file,

The download loop is working correctly but when am trying to save the product id & image path to a text file only the first record is saved.

i tried to print the output before using the file_put_contents function and it turns out am getting the correct result. I am wondering why only the first result is saved not all of them ?

I spent almost all day tryinf to figure it out but no luck. any help will be appreciated.

here is my code:

            $url = 'product-test2.json'; // path to  JSON file
            $data = file_get_contents($url);
            $characters = json_decode($data, true); 
            $i = array_column ($characters , 'Id');

            foreach ( $i as $value => $productid){
            include 'login.php';
            $url = "http://xxxx.com/api/products/image/";
            $url .= implode($matches [0]); // get the token form login.php
            $url .= '/' . $productid ;    //product id 
            // echo $url . '<br>';
            $opts = array ('http' => array ( 
                         'methos' => 'GET',
                         'header' => 'Content-typpe: application/json',
                         )
            );
            $context = stream_context_create ($opts);
            $urlImage = file_get_contents ($url , false , $context);
            $urlImage = substr_replace ($urlImage , "",-1);
            $urlImage = substr ($urlImage , 1);
            $fopenpath = 'C:\\xampp\htdocs\\test\\api\\images\\'   ;
            $fopenpath .= $productid . '.jpg';        
            $fp = fopen ( $fopenpath, 'w');
            $c = curl_init ($urlImage);    
            curl_setopt ($c , CURLOPT_FILE , $fp);
            curl_setopt ($c , CURLOPT_HEADER, 0);
            curl_setopt ($c , CURLOPT_POST, false);
            curl_setopt ($c , CURLOPT_SSL_VERIFYPEER, false);
            $rawdata = curl_exec ($c);
            fwrite ($fp, $rawdata);
            fclose ($fp);
            //
            $result3= ["id" => $productid ,"image_path" => $fopenpath]  ; 


            $image_file = "images.json";
            $output = json_encode ($result3);   
                print_r ($output);
            file_put_contents ($image_file , $output );  


            };

and here is the result from print_r ($result3);

 {"id":2977,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\2977.jpg"} {"id":2981,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\2981.jpg"} {"id":3009,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\3009.jpg"} {"id":3018,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\3018.jpg"} {"id":11531,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\11531.jpg"}

as you can see the Print_r is correct all what i want to do is to save the output into file and currently here is the result from file_put_contents only one array :

{"id":11531,"image_path":"C:\\xampp\\htdocs\\test\\api\\images\\11531.jpg"}

can you please help and tell me what am doing wrong i already spend a lot of time on this :)

  • 写回答

1条回答 默认 最新

  • dongza5150 2018-01-22 01:32
    关注

    file_put_contents is the same as opening, writing, and closing the file, which doesn't really make sense to do in a foreach loop. If you really want to use it, I think you could add the FILE_APPEND flag, which will stop it from overwriting itself over and over and over.

    An alternative would be to fopen before the foreach loop with the 'a' flag, which will create the file then keep appending lines instead of wiping out the file contents every loop. (And fclose after the foreach.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀