Upload and read a compressed file to populate a database
<p>To populate an sql database I upload .txt files with all data for each field and rows. When my internet connection is bad i have a lot of problems and sometimes process is aborted.
To fix the problem I'd upload .txt files in a compressed format(.zip or if possible also other formats: .rar ...etc). In this way, filesize is 15 times smaller and i can upload it easier and faster.
How can i do?</p>

<p>Here are codes i use at the moment:</p>

<p>upload.php</p>

<pre><code>  ...
  &lt;form action="readfile.php" enctype="multipart/form-data" method="post"&gt;
  &lt;br&gt;Filename:&amp;nbsp;&lt;input name="userfile" type="file"&gt;
  &lt;p&gt;&lt;input type="reset" value="Reset"&gt;
  &lt;input type="submit" value="Upload"&gt;
  &lt;/form&gt;
  ...
</code></pre>

<p>readfile.php</p>

<pre><code>  ...
  // Read data posted from form
$browser_name = $_FILES['userfile']['name'];
$temp_name = $_FILES['userfile']['tmp_name'];
$usrname = $_POST['uname'];

// Connect to the database
if (!($connection = @ mysql_connect ($hostName,
                    $username,
                    $password)))
    die ("Could not connect to database");

if (!mysql_select_db ($databaseName, $connection))
    showerror();

echo "&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"&gt;";
echo "&lt;html&gt;";
echo "&lt;head&gt;";
echo "&lt;title&gt;Upload file&lt;/title&gt;";
echo "&lt;/head&gt;";
echo "&lt;body&gt;";

// Was a file uploaded?
if (is_uploaded_file ($temp_name))
{
    echo "&lt;h1&gt;File Upload $browser_name&lt;/h1&gt;";
    echo "&lt;p&gt;Filename - $browser_name\n";
    echo "&lt;br&gt;User name - $uname\n";

    // Open the file
    if (!($file = fopen ($temp_name, "r")))
        die ("Could not open the file $file\n");

    // Read the first line
    $string = fgets ($file, 1024);
  ...
</code></pre>

<p>Where to modify and/or add codes?
If you need more details, please ask me and I paste more: I presumed they're the right lines to explain better everything and to modify for my needs.</p>

<p>Thank you so much in advance for your help.</p>

<p>Mattew</p>

<p>UPDATES:
I made some new modifies using ZipArchive. In a few words now the .zip file is uploaded in a folder and unzipped. So the nomefile.txt file can be read and processed as a normal .txt file inside the specific folder.
Unfortunately the fopen looks for a specific filename. My idea is now to rename any unzipped file (having a random name but .txt extension) to a specific file (for example myfile.txt). In this way i can fix also problems with "different" extensions having small/capitol letters such as .txt and .TXT</p>

<p>HERE IS THE CODE I MENTIONED BEFORE:
      ...</p>

<pre><code>  //unzip del file
   $zip = new ZipArchive;
   $zip-&gt;open('file.zip');
   $zip-&gt;extractTo('./');
   $zip-&gt;close();
   echo "File unzipped and ready to be processed";



    if (!($file = fopen ("namefile.txt", "r")))
        die ("Could not open the file $file\n");

    // Read the first line
    $string = fgets ($file, 1024);
    ....
</code></pre>

<p>Opinions? Helps?
Thanks in advance, I'm very new of php and maybe my problems are elementary, excuse me.</p>

<p>Bye.</p>
