Jump to content

[SOLVED] Uploading several files/images


JJohnsenDK

Recommended Posts

Hey

Im wondering if its possible to upload/update several files with the same code or shall i copy/paste the code 5 times?

Here is the code im using to upload a file.

[code]
<?php
$update_img = mysql_query("UPDATE
forside
  SET
billede1 = '".$_FILES['img1']['name']."',
billede2 = '".$_FILES['img2']['name']."',
billede3 = '".$_FILES['img3']['name']."',
billede4 = '".$_FILES['img4']['name']."',
billede5 = '".$_FILES['img5']['name']."'")
or die(mysql_error());

{
$tempfile = $_FILES['img1']['tmp_name'];
$destination = "../billeder/forside/{$_FILES['img1']['name']}";
move_uploaded_file($tempfile, $destination);
}
?>
[/code]

As you can see i have 5 tables and each of them should also upload a file to a folder with im using the above code for. But it only uploads the first file. Is it possible to rewrite that code so it uploads all the 5 files? or is there no ohter way than to copy/paste the code 5 times?

Link to comment
Share on other sites

You need to use a foreach loop really.  Never copy and paste the same code.  If the same code's being used more than once, then surely there's cause for a function there?

You obviously have a form that contains five fields called img1, img2, img3, img4, and img5.  First thing you need to do is change the names of these.  You want to submit an array rather than individual names, so rename them something like images[] (make sure whatever you call the field, you have the square brackets after it e.g.

[code]
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
[/code]

I'll post some sample code a little later so you can see something similar that I have.

Regards
Huggie
Link to comment
Share on other sites

I have a form that looks like this:

[size=8pt][b]form.htm[/b][/size]
[code]  <form name="upload" method="POST" action="process.php" enctype="multipart/form-data">
  <input type="file" name="imagefile[]"><br>
  <input type="file" name="imagefile[]"><br>
  <input type="file" name="imagefile[]"><br>
  <input type="file" name="imagefile[]"><br>
  <input type="file" name="imagefile[]"><br>
  <input type="submit" name="submit" value="Upload"><br><br>
  </form>[/code]

And the php that processes it like this:

[size=8pt][b]process.php[/b][/size]
[code]<?php
// If the form has been submitted then process it
if(isset($_POST['submit'])){

  // Perform this action for each of the uploaded files
  foreach ($_FILES['imagefile']['error'] as $k => $error){
      if ($_FILES['imagefile']['error'][$k] == 0){

        // Make sure the file is a jpeg
        if ($_FILES['imagefile']['type'][$k] == "image/jpeg" || $_FILES['imagefile']['type'][$k] == "image/pjpeg"){

            // Copy the temporary file to the original file name
            copy($_FILES['imagefile']['tmp_name'][$k], "images/".$_FILES['imagefile']['name'][$k]) or die ("Could not copy");
            echo "{$_FILES['imagefile']['name'][$k]}... Uploaded OK<br>\n";
            // You could put your database insertion code here.
        }
        else {
            echo "Could Not Copy, Wrong Filetype ({$_FILES['imagefile']['type'][$k]})<br>";
        }
      }
  }
}
?>
[/code]

Regards
Huggie
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.