Multiple file uploads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Multiple file uploads

Post 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.
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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:
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks, i'll give those a go
Post Reply