file_get_contents($ file)在PHP中的第二次迭代中执行循环时抛出警告

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

我有一些json格式的.txt文件。我试图逐个获取每个文件,修改它的内容,然后将这些内容放回到文件中。为此,我已经执行了一个循环,为每个文件执行此过程。循环在第一次迭代中运行绝对正常,但在第二次迭代中它会抛出一个警告:

“警告:file_get_contents(../ videos / list2.txt)[function.file-get-contents]:无法打开流:第85行的D:\ wamp \ www \ dtt_admin_V2 \ pages \ jason2.php中的参数无效” 。

file_get_contents(../videos/list2.txt)中的论点是正确的。它没有问题。我不能导致这个错误的原因。有关此错误的任何帮助都非常有用。带有错误的行在下面的代码块中用/ * line(#)* /等注释突出显示。

$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

php file-get-contents
1个回答
1
投票

我猜你的file_path_text中有[space]等特殊字符。您需要使用trim来删除path_text周围的空格并对path_text进行编码。

*详细信息:http://php.net/manual/en/function.file-get-contents.php

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