0

I have a number of json-format .txt files. I am trying to get each file one by one, modify it's contents and then put those contents back to the file. For this purpose I have executed a loop that does this process for every file. The loop runs absolutely fine in the first iteration but in the second iteration it throws a warning:

"Warning: file_get_contents(../videos/list2.txt ) [function.file-get-contents]: failed to open stream: Invalid argument in D:\wamp\www\dtt_admin_V2\pages\jason2.php on line 85".

The argument in file_get_contents(../videos/list2.txt) is correct. There is no issue in it. I can't what is causing this error. Any help regarding this error is much apperciated. Lines with error are highlighted with the comments like/* line(#)*/ in the below code block.

$sql = "SELECT * FROM dtt_location";

$result = $conn->query($sql);


if ($result->num_rows > 0) 
{    

while($row = $result->fetch_assoc()){

    $path_text=$row['path_text'];


    //echo "id_for_location".$id_for_location."<br>";

            $file = $path_text;
/*line48 */ $text = file_get_contents($file);

            //echo $text."<br>";
            $someObject = json_decode($text);
            $array_object=objectToArray($someObject);
            $ar_size=sizeof($array_object);
            $count=0;
            $array_name[]="";
            $array_url[]="";
            $skip=0;
            while($count<$ar_size){ 
                $temp_element=$array_object[$count];
                //print_r($temp_element);

                if($temp_element["name"]!=$video_name){
                    $array_name[$skip]=$temp_element["name"];
                    $array_url[$skip]=$temp_element["url"];
                    //$b=$a[0];
                    $skip++;
                }

                else{
                    //echo $count."<br>"."temp"."<br>".$temp_element["name"]."<br>".$temp_element["url"]."<br>";
                    //echo $count."<br>"."selected"."<br>".$video_name."<br>".$path_video."<br>";
                }

                $count++;
            }

            $count=0;
            $ar_si=$ar_size-1;


            //echo $ar_size."<br>";
            //echo $ar_si;
            $create[]="";
            while($count<$ar_si)
             {
                echo $count;

                $create[$count]=$first.$array_name[$count]. $second.$array_url[$count].$third;
                $count++;
            }   


            $create_j=join(",",$create);
            $create_j="[".$create_j."]";

            if($create_j=="[]"){
                //echo "$create_j"."<br>";
                $create_j=$text;
                //echo "$create_j";
                //error_msg_empty_broadcast();
    /*Line 108 */           file_put_contents($file, $create_j);
            }//echo $create_j."<br>";               
            else
            {
                file_put_contents($file, $create_j);


        }   
    }

Screen Shot of the error is

Blauharley
  • 4,186
  • 6
  • 28
  • 47
Mubeen Ul Haq
  • 33
  • 1
  • 9

1 Answers1

1

I guess that there are special charactors such as [space] in your file_path_text. You need to use trim to remove spaces around your path_text and encode your path_text.

*Detail in : http://php.net/manual/en/function.file-get-contents.php

Ngo Tuan
  • 205
  • 1
  • 2
  • 16