src/Tar.php
public function addFile($file, $fileinfo = '')
    {
        if (is_string($fileinfo)) {
            $fileinfo = FileInfo::fromPath($file, $fileinfo);
        }

        if ($this->closed) {
            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
        }

        $fp = @fopen($file, 'rb');
        if (!$fp) {
            throw new ArchiveIOException('Could not open file for reading: '.$file);
        }

        // create file header
        $this->writeFileHeader($fileinfo);

        // write data
        $read = 0;
        while (!feof($fp)) {
            $data = fread($fp, 512);
            $read += strlen($data);
            if ($data === false) {
                break;
            }
            if ($data === '') {
                break;
            }
            $packed = pack("a512", $data);
            $this->writebytes($packed);
        }
        fclose($fp);

        if($read != $fileinfo->getSize()) {
            $this->close();
            throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
        }

        if(is_callable($this->callback)) {
            call_user_func($this->callback, $fileinfo);
        }
    }
public function addFile($file, $fileinfo = '')
    {
        if (is_string($fileinfo)) {
            $fileinfo = FileInfo::fromPath($file, $fileinfo);
        }

        if ($this->closed) {
            throw new ArchiveIOException('Archive has been closed, files can no longer be added');
        }

        $fp = @fopen($file, 'rb');
        if (!$fp) {
            throw new ArchiveIOException('Could not open file for reading: '.$file);
        }

        // create file header
        $this->writeFileHeader($fileinfo);

        // write data
        $read = 0;
        while (!feof($fp)) {
            $data = fread($fp, 512);
            $read += strlen($data);
            if ($data === false) {
                break;
            }
            if ($data === '') {
                break;
            }
            $packed = pack("a512", $data);
            $this->writebytes($packed);
        }
        fclose($fp);

        if($read != $fileinfo->getSize()) {
            $this->close();
            throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
        }

        if(is_callable($this->callback)) {
            call_user_func($this->callback, $fileinfo);
        }
    }
public function addFile ( $ file , $ fileinfo = '' ) { if ( is_string ( $ fileinfo ) ) { $ fileinfo = FileInfo :: fromPath ( $ file , $ fileinfo ) ; } if ( $ this -> closed ) { throw new ArchiveIOException ( 'Archive has been closed, files can no longer be added' ) ; } $ fp = @ fopen ( $ file , 'rb' ) ; if ( ! $ fp ) { throw new ArchiveIOException ( 'Could not open file for reading: ' . $ file ) ; } // create file header $ this -> writeFileHeader ( $ fileinfo ) ; // write data $ read = 0 ; while ( ! feof ( $ fp ) ) { $ data = fread ( $ fp , 512 ) ; $ read += strlen ( $ data ) ; if ( $ data === false ) { break ; } if ( $ data === '' ) { break ; } $ packed = pack ( "a512" , $ data ) ; $ this -> writebytes ( $ packed ) ; } fclose ( $ fp ) ; if ( $ read != $ fileinfo -> getSize ( ) ) { $ this -> close ( ) ; throw new ArchiveCorruptedException ( "The size of $file changed while reading, archive corrupted. read $read expected " . $ fileinfo -> getSize ( ) ) ; } if ( is_callable ( $ this -> callback ) ) { call_user_func ( $ this -> callback , $ fileinfo ) ; } }
Add a file to the current TAR archive using an existing file in the filesystem

@param string $file path to the original file
@param string|FileInfo $fileinfo either the name to us in archive (string) or a FileInfo oject with all meta data, empty to take from original
@throws ArchiveCorruptedException when the file changes while reading it, the archive will be corrupt and should be deleted
@throws ArchiveIOException there was trouble reading the given file, it was not added
@throws FileInfoException trouble reading file info, it was not added
Add a file to the current TAR archive using an existing file in the filesystem