Processing files from zip and tar.bz2 files using Phar extension

As of php 5.3 the phar extension is built-in. For earlier versions can install using pear.

The following will open a .zip or .tar.bz2 file and loop through the files in specified archive directory.

The phar path can be treated like a disk directory.


    $uploaded_filename = $_FILES[ "userfile" ][ "tmp_name" ];
    $dest_path = '/path/to/file/' . time() . ".tar.bz2";
    
    // Move file from tmp to new location
    $success = move_uploaded_file( $uploaded_filename,
        $dest_path);

    // Access zip file and get files in archive directory /jobungo/
    $archive_path = "phar:///" . $dest_path . "/jobungo/";
    $files = scandir( $archive_path );
    
    // Loop through each .txt file and get its content
    foreach( $files as $file ) {
        $contents = file_get_contents( $archive_path . $file );
        
        // Split content lines into an array which can be used to parse the text data
        $contents_arr = explode("\n", $contents);
    }

Leave a comment

1 Comments.

  1. For most up-to-date news you have to visit world-wide-web and on world-wide-web I found this
    website as a best web page for most up-to-date updates.