Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何读取下一行的文件_Php_File - Fatal编程技术网

PHP如何读取下一行的文件

PHP如何读取下一行的文件,php,file,Php,File,您好,我有一个文件,每行有4个值,以:分隔,我现在试图从该文件中读取。该文件有多行,所有行的格式都相同,但由于某种原因,当我从中读取时,只有第一行被读取。我试过了 $baseFileHandle = fopen($baseFileLocation, 'r') or die("Error In Opening Inventory File Please Contact a System Developer"); $inline = fgets($baseFileHandle,

您好,我有一个文件,每行有4个值,以:分隔,我现在试图从该文件中读取。该文件有多行,所有行的格式都相同,但由于某种原因,当我从中读取时,只有第一行被读取。我试过了

$baseFileHandle = fopen($baseFileLocation, 'r') or die("Error In Opening Inventory File Please Contact a System Developer");
           $inline = fgets($baseFileHandle, 4096);
           if ($baseFileHandle) 
           {
               while (($line = fgets($baseFileHandle)) !== false) {
           list($lItem_id,$lItem_name,$lQuantity,$lThreshold) = explode(':',$inline);
               //var_dump($_POST);
               $temp_item = $this->model('Temp_Inventory');
               $temp_item->item_name = $lItem_name;
               $temp_item->quantity = $lQuantity;
               $temp_item->threshold = $lThreshold;
               $temp_item->inventory_id = $lItem_id;               
               $temp_item->insert(); 
           }

           fclose($baseFileHandle); 
以及

$inline = fgets($baseFileHandle, 4096);

            while (!feof($baseFileHandle) && !($found)) 
            {
                list($lItem_id,$lItem_name,$lQuantity,$lThreshold) = explode(':',$inline);
                //var_dump($_POST);
                $temp_item = $this->model('Temp_Inventory');
                $temp_item->item_name = $lItem_name;
                $temp_item->quantity = $lQuantity;
                $temp_item->threshold = $lThreshold;
                $temp_item->inventory_id = $lItem_id;               
                $temp_item->insert(); 
                fgets($baseFileHandle, 4096);
                //redirecttoaction
            }
            fclose($baseFileHandle);
但我似乎无法让它工作。我对php相当陌生,因此非常感谢您提供的任何帮助。

请尝试这样做:

$file = fopen($baseFileLocation, 'r') or die("Error In Opening Inventory File Please Contact a System Developer");

while (!feof($file)) {
    $inline = fgets($file, 4096);
    list($lItem_id,$lItem_name,$lQuantity,$lThreshold) = explode(':',$inline);
    $temp_item = $this->model('Temp_Inventory');
    $temp_item->item_name = $lItem_name;
    $temp_item->quantity = $lQuantity;
    $temp_item->threshold = $lThreshold;
    $temp_item->inventory_id = $lItem_id;               
    $temp_item->insert(); 
}

fclose($file); 
例如: