简体   繁体   English

将文件推送到循环中的数组

[英]Push file to array in loop

EDIT after all the answers, i updated the function, and it works 在所有答案之后编辑,我更新了功能,它的工作原理

I read out a importfolder. 我读出了一个导入文件夹。 In this folder are many different files available. 在此文件夹中有许多不同的文件可用。

  1. Step: I read the folder and add the files to a array 步骤:我读取文件夹并将文件添加到数组中
  2. Step: I open every file and try to import 步骤:我打开每个文件并尝试导入

When i cant import a file, then this happens, when another file in this row have to be imported first. 当我无法导入文件时,如果必须先导入此行中的另一个文件,则会发生这种情况。

Example: If I open a file "message to a address", this could not be imported, when the address are not added into the database. 示例:如果我打开文件“消息到地址”,则在地址未添加到数据库中时无法导入。 But in some other file of this filelist is the "create address"-file. 但是在这个文件列表的其他文件中是“创建地址”文件。 When this is created, then it is good, when the "message to a address" will be added to the filelistarray on the end. 当创建它时,那么当“消息到地址”将被添加到文件列表阵列的末尾时,这是好的。

My Code give me an offset problem: 我的代码给了我一个补偿问题:

function importData( $path, $db, $mail )
{
    //Get available Importfiles
    $filelist = getFilelist( $path );
    for ($i = 0; $i < count($filelist); $i++)
    {
        $filename = $path . "/" . $filelist[$i];
        $file = fopen( $filename,"r" );

        while(!feof( $file )) {
            $items = explode( ";", fgets( $file ) );

            //Get messagetyp
            if( strtolower(trim($items[0])) == "nachrichtentyp" )
            {
                $messagetyp = $items[1];
                break;
            }
        }
        fclose($file);
        if ( $messagetyp )
        {
            $f = "import" . $messagetyp;
            if( !$f($filename, $db, $mail) )
            {
                array_push($filelist, $filelist[$i]);

            }
        }
    }
}

This my error, when I push the element to the the filelist-array 这是我的错误,当我将元素推送到filelist-array时

PHP Warning:  feof() expects parameter 1 to be resource, boolean given in /var/www/symfony/importscript/import.php on line 37
PHP Warning:  fgets() expects parameter 1 to be resource, boolean given in /var/www/symfony/importscript/import.php on line 38

你肯定应该检查fopen返回另一个值而不是FALSE,可能其中一个文件不存在或者你受到限制。

According to your errors, problem lies not in array_push but in fopen() : 根据你的错误,问题不在于array_push而在于fopen()

$file = fopen( $filename,"r" );

If php fails to open that file, variable $file will be set to false and because of that feof() and fgets() will give you errors. 如果php无法打开该文件,变量$ file将被设置为false ,因为feof()fgets()会给你错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM