0

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

2
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by viktor.jiracek.5
0

@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]
            );
0

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");
    }
}
?>
0

@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.

Edited by LastMitch: grammer

0

@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.

Edited by LastMitch: grammer

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.