0

I'm trying to code a script which makes it possible to upload multiple images. This is the code I'm working with.

if(isset($_POST['submit_images'])) {
    $i = 0;
    foreach($_FILES['file'] as $file) {
        $image = new Image();
        $image->member_id = $_POST['id'][$i];
        $image->image_type = "MedlemsBillede";
        $image->attach_file($_FILES['file']);
        if($image->save()) {
            $message = 'Billedet blev uploadet med succes.';
        } else {
            $message  = join("<br />", $image->errors);
        }
        $i++;
    }
}

My problem lies in this call to function:

$image->attach_file($_FILES['file']);

And that function looks like this:

public function attach_file($file) {
    if(!$file || empty($file) || !is_array($file)) {
        $this->errors[] = "Der blev ikke uploadet nogen fil.";
        return false;
    } elseif($file['error'] != 0) {
        $this->errors[] = $this->upload_errors[$file['error']];
        return false;
    } else {
        $this->temp_path    = $file['tmp_name'];
        $this->file_name    = str_replace(' ', '_', basename($file['name']));
        $this->file_type    = $file['type'];
        $this->file_size    = $file['size'];
        $this->ts   = date('Y-m-d H:i:s');
        return true;
    }
}

Does anybody have an idea for how to fix this?

2
  • My problem lies in this call to function ... what is the problem ? any errors ? what happens when its executed ?
    – Manse
    Jul 19, 2012 at 15:54
  • I'm not able to send to correct part of the array through to the attach_file function. It works when I'm only using the function for the uploading of one file. Jul 19, 2012 at 16:00

1 Answer 1

0

I think the problem is in this line:

$image->attach_file($_FILES['file']);

You are iterating over $_FILES['file'] rather than just $_FILES

Also you are the calling the array through which you are iterating inside the loop

The call should be like this

$image->attach_file($file);

May this help you

3
  • Yes I've tried that too but that gives me "Undefined index" errors on the following: error,tmp_name,name,type,size Jul 19, 2012 at 16:09
  • you some debugging skills like try doing this print_r($_FILES) just to check what values the array contains
    – rockstar
    Jul 19, 2012 at 16:15
  • Array ( [file] => Array ( [name] => Array ( [0] => cup32.png [1] => cardgame32.png [2] => boxdownload32.png ) [type] => Array ( [0] => image/png [1] => image/png [2] => image/png ) [tmp_name] => Array ( [0] => /Applications/MAMP/tmp/php/phpdkIp2J [1] => /Applications/MAMP/tmp/php/phpKwtbda [2] => /Applications/MAMP/tmp/php/phpquOEWC ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [size] => Array ( [0] => 433 [1] => 696 [2] => 1035 ) ) ) Jul 19, 2012 at 16:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.