Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Location
    Montreal, CA
    Posts
    32

    Unanswered: Unzip .csv before uploading

    Hello,

    I am familiar with the downloading of a csv from a url to insert into a mysql table.

    I seem to be having difficulty now that the csv is zipped.
    after doing:
    a) $tempVar = file_get_contents($url);
    b) $print = explode("\n",$tempVar);
    c) $subPrint = explode (",",$print[$i]);

    I obtain strange characters.

    Can anyone suggest a solution?

    Thanks

  2. #2
    Join Date
    May 2008
    Posts
    277
    You need to unzip the csv, otherwise you're just going to end up with binary gobbledy-gook.

    The zip extension will probably be of use.

  3. #3
    Join Date
    Feb 2010
    Location
    Montreal, CA
    Posts
    32
    Quote Originally Posted by futurity View Post
    You need to unzip the csv, otherwise you're just going to end up with binary gobbledy-gook.

    The zip extension will probably be of use.

    Thanks for the prompt response.
    I glanced at it but only got around to the programing just now.

    Do you have a practical example of how I can extract a csv from a zip?
    I'm not sure the examples here PHP: Examples - Manual are what I need.

    Should I be using the ExtractTo method? PHP: ZipArchive::extractTo - Manual
    Is this necessary? My preference is to simply "read" the csv so i can dump each line into a table.

    Thanks.

  4. #4
    Join Date
    May 2008
    Posts
    277
    I've not personally used this extension, nor handled zip file uploads, so you'll have to do some experimentation on your own.

    The zip functions seem easier and more intuitive to use, not to mention the class-based API doesn't seem to offer any way to directly read an archive without first unzipping it. But I can't tell whether or not the functions are an old and/or depreciated API.

    Anyway, I imagine you'd do something like:
    PHP Code:
    $dir zip_open('file');
    $entry zip_read($dir);
    $clear zip_entry_read($entry); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •