fgetcsv example

$filePath = $file->getRealPath();
        if (!$filePath) {
            throw new \RuntimeException('File does not exists');
        }

        $fileHandle = fopen($filePath, 'rb');
        if (!$fileHandle) {
            throw new FileNotReadableException($filePath);
        }

        // read the first CSV line         $record = fgetcsv($fileHandle, 0, $delimiter$enclosure$escape);
        fclose($fileHandle);
        if (empty($record) || $record[0] === null) {
            throw new InvalidFileContentException($file->getFilename());
        }

        // construct the mapping from the given CSV line data         $definition = $this->definitionInstanceRegistry->getByEntityName($sourceEntity);
        $keyLookupTable = $this->getKeyLookupTable($context$sourceEntity);

        $mappings = new MappingCollection();
        foreach ($record as $index => $column) {
            

  protected function readMultilingualContent($filename) {
    $default_content_path = $this->module_path . "/default_content/languages/";

    // Get all enabled languages.     $translated_languages = $this->enabledLanguages;

    // Load all the content from any CSV files that exist for enabled languages.     foreach ($translated_languages as $language) {
      if (file_exists($default_content_path . "$language/$filename") &&
      ($handle = fopen($default_content_path . "$language/$filename", 'r')) !== FALSE) {
        $header = fgetcsv($handle);
        $line_counter = 0;
        while (($content = fgetcsv($handle)) !== FALSE) {
          $keyed_content[$language][$line_counter] = array_combine($header$content);
          $line_counter++;
        }
        fclose($handle);
      }
      else {
        // Language directory exists, but the file in this language was not found,         // remove that language from list of languages to be translated.         $key = array_search($language$translated_languages);
        
$profile = new Profile('token'.$i);
            $profile->setIp('127.0.0.'.$i);
            $profile->setUrl('http://foo.bar/'.$i);

            $this->storage->write($profile);
            $this->storage->write($profile);
            $this->storage->write($profile);
        }

        $handle = fopen($this->tmpDir.'/index.csv', 'r');
        for ($i = 0; $i < $iteration; ++$i) {
            $row = fgetcsv($handle);
            $this->assertEquals('token'.$i$row[0]);
            $this->assertEquals('127.0.0.'.$i$row[1]);
            $this->assertEquals('http://foo.bar/'.$i$row[3]);
        }
        $this->assertFalse(fgetcsv($handle));
    }

    /** * @dataProvider provideExpiredProfiles */
    public function testRemoveExpiredProfiles(string $index, string $expectedOffset)
    {
if (str_starts_with($data, self::UTF8_BOM)) {
            fseek($handle, \strlen(self::UTF8_BOM));
        }

        $headers = null;
        $nbHeaders = 0;
        $headerCount = [];
        $result = [];

        [$delimiter$enclosure$escapeChar$keySeparator, , , , $asCollection] = $this->getCsvOptions($context);

        while (false !== ($cols = fgetcsv($handle, 0, $delimiter$enclosure$escapeChar))) {
            $nbCols = \count($cols);

            if (null === $headers) {
                $nbHeaders = $nbCols;

                if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
                    for ($i = 0; $i < $nbCols; ++$i) {
                        $headers[] = [$i];
                    }
                    $headerCount = array_fill(0, $nbCols, 1);
                } else {
                    
// Open file, read through looking for matching credentials         $fp = @fopen($this->_file, 'r');
        if (!$fp) {
            /** * @see Zend_Auth_Adapter_Http_Resolver_Exception */
            throw new Zend_Auth_Adapter_Http_Resolver_Exception('Unable to open password file: ' . $this->_file);
        }

        // No real validation is done on the contents of the password file. The         // assumption is that we trust the administrators to keep it secure.         while (($line = fgetcsv($fp, 512, ':')) !== false) {
            if ($line[0] == $username && $line[1] == $realm) {
                $password = $line[2];
                fclose($fp);
                return $password;
            }
        }

        fclose($fp);
        return false;
    }
}
if (str_starts_with($data, self::UTF8_BOM)) {
            fseek($handle, \strlen(self::UTF8_BOM));
        }

        $headers = null;
        $nbHeaders = 0;
        $headerCount = [];
        $result = [];

        [$delimiter$enclosure$escapeChar$keySeparator, , , , $asCollection] = $this->getCsvOptions($context);

        while (false !== ($cols = fgetcsv($handle, 0, $delimiter$enclosure$escapeChar))) {
            $nbCols = \count($cols);

            if (null === $headers) {
                $nbHeaders = $nbCols;

                if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
                    for ($i = 0; $i < $nbCols; ++$i) {
                        $headers[] = [$i];
                    }
                    $headerCount = array_fill(0, $nbCols, 1);
                } else {
                    


    /** * @param resource $resource */
    private function readSingleRecord($resource, int $offset): ?array
    {
        $this->seek($resource$offset);

        while (!feof($resource)) {
            $this->handleBom($resource);
            $record = fgetcsv($resource, 0, $this->delimiter, $this->enclosure, $this->escape);
            // skip if it's an empty line             if ($record === false || (\count($record) === 1 && $record[0] === null)) {
                continue;
            }

            $record = $this->mapRecord($record);

            // skip empty             if ($record === null || array_filter($record) === []) {
                continue;
            }

            

    protected function _loadTranslationData($filename$locale, array $options = array())
    {
        $this->_data = array();
        $options     = $options + $this->_options;
        $this->_file = @fopen($filename, 'rb');
        if (!$this->_file) {
            throw new Zend_Translate_Exception('Error opening translation file \'' . $filename . '\'.');
        }

        while(($data = fgetcsv($this->_file, $options['length']$options['delimiter']$options['enclosure'])) !== false) {
            if (substr($data[0], 0, 1) === '#') {
                continue;
            }

            if (!isset($data[1])) {
                continue;
            }

            if (count($data) == 2) {
                $this->_data[$locale][$data[0]] = $data[1];
            } else {
                
Home | Imprint | This part of the site doesn't use cookies.