Page 1 of 1

Multiple file uploads

Posted: Sun Jan 11, 2004 1:05 pm
by therat
I want to be able to upload 2 files using the same form. The first file will be a jpg of a certain size. If it's not the correct size it needs to be resized so that it is. The second file will be a zip/rar archive file.
Once uploaded they should both be renamed so that they have the same name and, the info entered into a database.
I have no problem with the actual uploading & inserting into the database but, any suggestions or tutorials on combining the rest appreciated.

Posted: Sun Jan 11, 2004 2:39 pm
by basdog22

Code: Select all

<?php
$path_to_file = "templates/$title";
      $files = $HTTP_POST_FILES['files'];
      if (!ereg("/$", $path_to_file))
      $path_to_file = $path_to_file."/";
      foreach ($files['name'] as $key=>$name)
      {
        if ($files['size'][$key])
        {
        // clean up file name
          $name = ereg_replace("[^a-z0-9._]", "",str_replace(" ", "_",str_replace("%20", "_", strtolower($name))));
          $location = $path_to_file.$name;
          while (file_exists($location))
          $location .= ".copy";
          copy($files['tmp_name'][$key],$location);
          unlink($files['tmp_name'][$key]);
          echo "\n
          Successfully uploaded file: $name.";
        }
      }
?>
You can use substr() to grub the last 3 letters : eg zip and then store them in $extension. Then give the $name var a new name and save as $name.$extension.

Then the database thing is simple query

Posted: Sun Jan 11, 2004 2:39 pm
by DuFF
It's pretty simple, just do the same thing you would with one upload and then repeat. Heres an example.

form.php

Code: Select all

<form action="upload.php" method="post">
<input type="file" name="image">
<input type="file" name="zipfile">
<input type='submit' name='submit' value='Submit'>
</form>
upload.php

Code: Select all

<?php
echo $_FILES['image']['name']; //echos something like somepicture.jpg
echo $_FILES['zipfile']['name']; //echos seomthing like data.zip
?>
You can then manipulate them however you want.

Edit:
Damn, Basdog beat me to it :wink:

Posted: Sun Jan 11, 2004 2:50 pm
by therat
Thanks, i'll give those a go