Ered
Ered

Reputation: 497

combine and upload multiple inputs with foreach

i have this code that submits multiple files and titles im trying to combine the results to upload to my DB and check if title[] is empty and print a custom value but im having problems with the title[] i need to combine with the s_upload[] array:

<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){

foreach ($_FILES["s_upload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
        $name = $_FILES["s_upload"]["name"][$key];
       // move_uploaded_file($tmp_name, "data/$name");

       if ($_POST['title']==''){
       echo 'Title';
       }else{
       print_r ($_POST['title']);
       echo $name;
       }
    }
}   
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

</head>
    <body>


<form method="post" enctype="multipart/form-data">
<div class='file_upload' id='f1'>
<input type="text" name="title[]" id="t1">
<input size="14" name='s_upload[]' id="i1" type='file'/>
</div>
<div class='file_upload' id='f2'>
<input type="text" name="title[]" id="t2">
<input size="14" name='s_upload[]' id="i2" type='file'/>
</div>
<input type="submit"/>
</form>


    </body>
</html>

when i submit this is are the results:

Array ( [0] => 11111 [1] => 22222 ) 1.jpgArray ( [0] => 11111 [1] => 22222 ) 2.jpg

i need this results if title exists:

1111 1.jpg 
2222 2.jpg

and this results if title is empty:

Title 1.jpg 
2222 2.jpg

Upvotes: 2

Views: 869

Answers (3)

jedwards
jedwards

Reputation: 30210

Since you didn't specify where the 1111 came from, I'll give you a few options:

<?
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
    foreach ($_FILES["s_upload"]["name"] as $key => $name) {
        if ($_FILES["s_upload"]["error"][$key] == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];

            if ($_POST['title'][$key]==''){
                // No title was specified: construct default title
                // This defaults the title to the filename of the file that was uploaded
                $title = $name;
                // This defaults the title to some random 32-character hex string
                $title = md5(time()+rand()); 
            }
            else{
                // A title was specified in the input box: use it
                $title = $_POST['title'][$key];
            }
            echo "$title $name<br />";
        }
    }
}
?>

Here's what I changed:

  1. I'm now looping over the name instead of the error. It just makes a lot more sense to do it this way. Looping over the error was a source of confusion.
  2. Since you named your inputs using an array (the []) in the input name, you have to specify which one you're referring to in your code. This means you need to use $key consistently throughout.
  3. You said you wanted a default title when one wasn't specified, but didn't explain how you want it constructed. I threw two random ideas in there. The first defaults to the filename of the uploaded file. The second is just a random string. You could change this to something based off the current time, the IP of the uploader, etc. etc. In any case, you'd do it there.

Upvotes: 2

StasGrin
StasGrin

Reputation: 1810

<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
$i = 0;
foreach ($_FILES["s_upload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
        $name = $_FILES["s_upload"]["name"][$key];
       // move_uploaded_file($tmp_name, "data/$name");

       if ($_POST['title'][$i]==''){
       echo 'Title '.$name;
       }else{
       echo $_POST['title'][$i] . ' ' . $name."\n";
       }
    }
    $i++;
}   
}
?>

This code is bad, but i just make it work, pls at least make it more readable.


PsyK edit: I updated the code to remove the need for $i as that number was already stored in $key. All that you was missing was to reference the title as an array, just like you was for the files uploaded.

<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ){
foreach ($_FILES["s_upload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["s_upload"]["tmp_name"][$key];
        $name = $_FILES["s_upload"]["name"][$key];
       // move_uploaded_file($tmp_name, "data/$name");

       if ($_POST['title'][$key]==''){
       echo 'Title '.$name;
       }else{
       echo $_POST['title'][$key] . ' ' . $name."\n";
       }
    }
}   
}
?>

Upvotes: 3

PsyKzz
PsyKzz

Reputation: 740

Just looking at your code try this...

It may work, or it may not, i've not tested it myself.

  if (is_array($_FILES) && !empty($_FILES)) {
     foreach ($_FILES['s_upload'] as $key => $file) {
        if ($file['error']!=UPLOAD_ERR_OK)
           continue;

        $tmp_name = $file['tmp_name'];
        $name = $file['name'];

        if (isset($_POST['title'][$key])) { # Standard method.
           $title = $_POST['title'][$key];
        } else {
           $title = "Default title";
        } 

        // Do what you need to do with the stuff.

     }
  }

Upvotes: 1

Related Questions