Hello all,
I am working on an upload script. This one is taken straight from php.net. I tried to get it to work but can't seem to. Could you help out?
This is the code:

<?php

$uploads_dir = 'file:///PETBookPro09/Users/petuser1/Desktop/';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

This is the error message it says:
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/....(stuff).../uploader1.php on line 142

I would appreciate any help at all! Thanks

Recommended Answers

All 6 Replies

Member Avatar for LastMitch

@viktor.jiracek.5

Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/....(stuff).../uploader1.php on line 142

foreach ($_FILES["pictures"]["error"] as $key => $error)

It means it's not an array. Another words you need to create an array in order for it to work. Something like this or similar to this, hopefully it will give you an idea how to write it and also give you an idea how it works:

        $files = array();
        foreach($entry['name'] as $key => $name) {
            $files[$key] = array(
                'name' => $name,
                'tmp_name' => $entry['tmp_name'][$key],
                'size' => $entry['size'][$key],
                'type' => $entry['type'][$key],
                'error' => $entry['error'][$key]
            );

Thanks for the quick response. So, in order to fix the problem... the code should look like this?:

<?php
$uploads_dir = 'file:///PETBookPro09/Users/petuser1/Desktop/';
 $files = array();
         foreach($entry['name'] as $key => $name) {
             $files[$key] = array(
                 'name' => $name,
                 'tmp_name' => $entry['tmp_name'][$key],
                 'size' => $entry['size'][$key],
                 'type' => $entry['type'][$key],
                 'error' => $entry['error'][$key]
             );
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>
Member Avatar for LastMitch

@viktor.jiracek.5

No, that's not right. Have you at least try to write it out first then show me what you did before you reply back? I feel like you just copy and paste it and show it to me. The example I posted is an array. No it won't work like that.

You have to create a if statement in order it to work with the example I posted.

Member Avatar for LastMitch

@viktor.jiracek.5

I feel like you might not know how to write it. So this is an example how this array looks like and how it works:

print_r($_FILES);

Array(
[pictures] => Array(

[name] => Array(
[0] => 001.jpg
[1] => 002.jpg
[2] => 003.jpg
)

[type] => Array(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)

[tmp_name] => Array(
[0] => /tmp/phpMX74IR
[1] => /tmp/phpkKSkL9
[2] => /tmp/php041QAv
)

[error] => Array(
[0] => 0
[1] => 0
[2] => 0
)

[size] => Array(
[0] => 46433
[1] => 412167
[2] => 356231
)

))

-

$uploads_dir = 'file:///PETBookPro09/Users/petuser1/Desktop/';

foreach($_FILES['pictures']['name'] as $key=>$row){
if ($error == UPLOAD_ERR_OK) {
$name = $row;
$type = $_FILES['pictures']['type'][$key];
$tmp_name = $_FILES['pictures']['tmp_name'][$key];
$error = $_FILES['pictures']['error'][$key];
$size = $_FILES['pictures']['size'][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
 }
 }

Now try to figure it out. I gave you 3/4 of the script. The 1/4 is to combine code and make it work meaning you need to figure it out or at least put some effort to figure it out. I layout foundation already. If you have any questions just post it here.

I am working on the script. I am not going to have users upload pictures, though. Is that a problem? They will be uploading lots of different file types.

I worked with another script and got that one working. Thanks for your guys' help though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.