Warning: fopen() expects parameter 1 to be string, array given in /home/speedycm/public_html/speedyautos/carphoto.php on line 42

Warning: filesize() [function.filesize]: stat failed for Array in /home/speedycm/public_html/speedyautos/carphoto.php on line 43

Warning: fread(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 43

Warning: fclose(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 44

i keep getting these error messages whenever trying to upload a picture on my website and i'm not sure how to sort them out. can anyone please help? lines 36-59 read:

$CarInfo->Load();
if ($hidaction == "addphoto")
{
    $ctrP = 0;
    foreach ($_FILES['pics'] as $pics)
    {
        if ($_FILES['pics']['name'][$ctrP] <> "")
        {
            if (is_uploaded_file($_FILES['pics']['tmp_name'][$ctrP]) or die("No Image: " . $_FILES['pics']['name'][$ctrP]))
            {
                $ext = substr(strrchr($_FILES['pics']['name'][$ctrP], "."), 1);
                $fp = fopen($_FILES["pics"]["tmp_name"], 'rb');
                $contents = fread($fp, filesize($_FILES["pics"]["tmp_name"]));
                fclose($fp);
                if (preg_match("/system/", $contents) OR preg_match("/<\?/", $contents))
                {
                    $error .= "Invalid image: {$_FILES['pics']['name'][$ctrP]}<br />";
                    //$pieces = explode(".", $_FILES['pics']['name'][$ctrP]);
                    //$ext = $pieces[count($pieces) - 1];
                } elseif ((in_array($ext, $types_array)) AND ($_FILES['pics']["size"][$ctrP] < (MAXFILE_SIZE * 1000000)))
                {
                    $orgImageName = "cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);
                    $thmImageName = "thumb_cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);
                    $dtlImageName = "dtl_cid" . $property_id . "_" . str_replace(" ", "_", $_FILES['pics']['name'][$ctrP]);

many thanks in advance!

Dani AI

Generated

Root cause and quick diagnosis
The chain of warnings is caused by passing an array where a string file path is expected. In a multiple-file upload, PHP sets $_FILES['pics']['tmp_name'] (and the other fields) as arrays; calling functions like fopen() or filesize() on the whole $_FILES['pics']['tmp_name'] triggers “expects parameter 1 to be string, array given” and the subsequent fread()/fclose() failures. As suggested, inspecting the structure (print_r) confirms the tmp_name field is an array.

Looping mismatch
The code mixes a foreach over the top-level $_FILES with index-based accesses to $_FILES['pics'][...], which is the confusion pointed out by . Either iterate the per-file indexes (0..count-1) for the 'pics' subarrays, or foreach the inner subarray while using the corresponding index; do not pass the whole array into fopen/filesize — always pass the per-file tmp_name string.

Validation and safety checklist

  • Ensure each file reports UPLOAD_ERR_OK before using it.
  • Use is_uploaded_file() and then move_uploaded_file() to save files (don’t rely on fopen/fread for moving).
  • Validate image files with getimagesize() or finfo_file() rather than trusting extensions.
  • Use $_FILES['pics']['size'][$i] for size checks (filesize() on tmp may be less reliable).
  • Sanitize and uniquify filenames, store uploads outside the web root or disable PHP execution in the uploads dir, and enforce allowed MIME types.

Minimal pattern example (index-based)

$files = $_FILES['pics'];
$count = count($files['name']);
for ($i = 0; $i < $count; $i++) {
    if ($files['error'][$i] !== UPLOAD_ERR_OK) { continue; }
    $tmp = $files['tmp_name'][$i];
    if (!is_uploaded_file($tmp)) { continue; }
    if (getimagesize($tmp) === false) { continue; } // not an image
    $safe = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($files['name'][$i]));
    move_uploaded_file($tmp, __DIR__ . '/uploads/' . $safe);
}

This resolves the array-vs-string errors and implements stronger validation and safer storage.

Recommended Answers

All 2 Replies

Member Avatar for Member #380484

If you do a

print_r($_FILES["pics"]["tmp_name"]);

I'm sure you'll see that it is an array as passed in through multiple form values <input name="photo[]" /> or similar.

In other words,
Here you treat it like an array

is_uploaded_file($_FILES['pics']['tmp_name'][$ctrP])

and here you treat it like a string, but clearly it's an array -- and that is the cause of your errors

$fp = fopen($_FILES["pics"]["tmp_name"], 'rb');

* A side note. When submitting a question it is useful, to both the answerer and also yourself, to reduce the problem into the simplest possible example (not just copy/paste directly from your code). Often this will be enough to solve the answer on your own, but even if not, will make it easier to read the situation. Tnx.

Member Avatar for Member #120589

I'm confused:

you do this: foreach ($_FILES as $pics)

but keep on using $_FILES in your loop code. Surely you want to be using $pics?

e.g.

if ($pics['name'][$ctrP] <> "")

If have to say I don't understand the need for [$ctrP]

Is this supposed to relate to a array item counter? If so, you don't need it as $pics is already doing the loop.

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.