1

SOLVED! Used this example from php.net post method handling multiple uploads, with the knowledge gained by using andre's answer which is why I selected it as the answer, here it goes!

foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
  if ($x=="0"){
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id].".".$data[1];
    $x=$x+1;
    }else{
    $data=explode(".",$_FILES["userfile"]["name"][$key]);
    $named=$id[id]."-".$x.".".$data[1];
    $x=$x+1;
    }
    $uploaddir = '/home/content/92/8498392/html/items/'.$_POST[catid];
    $tmp_name = $_FILES["userfile"]["tmp_name"][$key];
    move_uploaded_file($tmp_name, "$uploaddir/$named");
}

}

The result is the first image is uploaded as 45.jpg, the second is uploaded as 45-1.jpg and so forth! Thanks for the help guys :D

5
  • 2
    It's nice that it reported an error. PHP is pretty good about reporting errors, unfortunately it's near impossible for us to decipher what the error says or means unless you post it. Commented Feb 1, 2012 at 2:06
  • @Francis :unfortunately I'm self taught and am really bad at error reporting. Usually I can figure out on my own whats wrong but the error simply says error. I removed that part of the script and now it completes and updates the database but won't upload the files. The files are no bigger then 1M and arent breaching the limit. And I'm only uploading five files and the file limit is 20. The dir path works for mkdir so I'm not sure whats up. Perhaps the uploaded file is wrong? Commented Feb 1, 2012 at 2:10
  • Look in your PHP log file and it should show you a detailed message of what the error is. Your error log is going to be defined in your website's conf file, or if you're on a shared hosting server, you can probably only get to it through the provided control panel. Commented Feb 1, 2012 at 2:53
  • @Francis I checked my control panel and my ftp for error logs and could only find bot error logs from bing and google etc. No php error logs. Commented Feb 1, 2012 at 3:18
  • You may need to ask your host. Hosting providers should always give you access to your error logs. Commented Feb 1, 2012 at 3:48

3 Answers 3

2

The problem is how you are iterating over the $_FILES array. It does not handle mutliple files like you think so use the functions below and incorporate them into your script. The credit for the script below goes to the guy on the PHP site. Link is below the code.

function multiple(array $_files, $top = TRUE)
{
    $files = array();
    foreach($_files as $name=>$file){
        if($top) $sub_name = $file['name'];
        else    $sub_name = $name;

        if(is_array($sub_name)){
            foreach(array_keys($sub_name) as $key){
                $files[$name][$key] = array(
                    'name'     => $file['name'][$key],
                    'type'     => $file['type'][$key],
                    'tmp_name' => $file['tmp_name'][$key],
                    'error'    => $file['error'][$key],
                    'size'     => $file['size'][$key],
                );
                $files[$name] = multiple($files[$name], FALSE);
            }
        }else{
            $files[$name] = $file;
        }
    }
    return $files;
}

print_r($_FILES);
/*
Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 400.png
                )
            [type] => Array
                (
                    [0] => image/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php5Wx0aJ
                )
            [error] => Array
                (
                    [0] => 0
                )
            [size] => Array
                (
                    [0] => 15726
                )
        )
)
*/
$files = multiple($_FILES);
print_r($files);
/*
Array
(
    [image] => Array
        (
            [0] => Array
                (
                    [name] => 400.png
                    [type] => image/png
                    [tmp_name] => /tmp/php5Wx0aJ
                    [error] => 0
                    [size] => 15726
                )
        )
)
*/
?>

I took this from the PHP website

Sign up to request clarification or add additional context in comments.

3 Comments

@Andrew Honsberg ok, so this will return the array $files which will have the values $files[name] that holds the name and $files[tmp_name] that I can then use to move the tmp file to the new folder and change the name the same way I was doing before except with no foreach? I read a lot of those examples on php.net but couldn't understand them which is why I came here. I appreciate the code, I will run some tests and try to make use of it. If anyone can set me up with a rehash of what I did that'd be awesome too :D Thanks again andre.
I did what you said and I think I understand it finally, but I am getting an error when trying to iterate through files after calling the multiple function. It should work within my previous script now, if I understand correctly. Is this wrong? foreach (multiple($_FILES) as $file) { if($x="0"){ $data=explode(".",$files['name']); $name=$id.".".$data[1]; $uploaddir = '/home/content/92/8498392/html/items/'.$_POST[catid].'/'; $uploadfile = $uploaddir . $name; move_uploaded_file($files['tmp_name'], $uploadfile); echo $uploadfile; $x=$x+1; }
i get an unexpected $end error at the end of the page but it disappears when I remove the offending foreach statement which makes me assume that I am missing something here. After manipulating the array from a 3 step array into a 2 step array using the multiple function, I should be able to iterate through it with that script, correct???
1
<?php
$id = 877;
$x = 0;
foreach ($_FILES as $file) {
  $data = explode(".", $file['userfile']['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['userfile']['tmp_name'], $filePath);
  $x++;
}
?>

Try that.

2 Comments

Tried it, unfortunately it still won't work. It doesn't upload ANY of the files! This is my form field for the photos if it helps, they are all the same which I think is how it works. <input name="userfile[]" type="file">
unfortunately it isn't uploading anything. I also stuck echo $filepath; after the move_uploaded_file and nothing prints from it... x is set to 0 just like your script as well.
1

Just use the function I supplied and incorporate like below:

<?php
$id = 877;
$x = 0;
$_FILES = multiple($_FILES); // taken from above snippet from php.net
foreach ($_FILES as $fileKey => $file) {
  $data = explode(".", $file['name']);
  $fileExtension = $data[count($data)-1];
  if ($x == 0) {
    $filePath = '/home/content/92/8498392/html/items/' . $_POST['catid'] . '/' . $id . '.' . $fileExtension; 
  }
  else {
    $filePath = '/home/content/92/8498392/html/items/'.$_POST['catid'].'/' . $id . '-' . $x . '.' . $fileExtension;
  }
  move_uploaded_file($file['tmp_name'], $filePath);
  $x++;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.