0

I have this form:

<form action="image_upload.php" method="post" enctype="multipart/form-data">
   Image 1: <input type="file" name="event_image" />
   <input type="submit" />
</form> 

and this php code (image_upload.php):

print_r($_FILES);
if ((($_FILES["event_image"]["type"] == "image/jpeg")
|| ($_FILES["event_image"]["type"] == "image/pjpeg"))
&& ($_FILES["event_image"]["size"] < 200000))
  {
  if ($_FILES["event_image"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["event_image"]["error"] . "<br />";
    }
  else
    {
    if (file_exists("/images/events/" . $_FILES["event_image"]["name"]))
      {
      echo $_FILES["event_image"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["event_image"]["tmp_name"],
      "/images/events/" . $_FILES["event_image"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["event_image"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

I have no idea where this is going wrong as I've had the same code working before.

I am getting the following error though...

Array ( [event_image] => Array ( [name] => my_image.jpg [type] => image/jpeg [tmp_name] => /private/var/tmp/phpvIYmAZ [error] => 0 [size] => 48512 ) )

Warning: move_uploaded_file(../../../images/events/my_image.jpg): failed to open stream: Permission denied in /path/event_upload.php on line 25

Warning: move_uploaded_file(): Unable to move '/private/var/tmp/phpvIYmAZ' to '../../../images/events/my_image.jpg' in /path/event_upload.php on line 25 Stored in: upload/my_image.jpg

Notice: Undefined index: event_image in /path/event_upload.php on line 57

2
  • 1
    print_r($_FILES) at the top of it, please. Also, on the if you should add the condition isset($_FILES['event_image']) as the first condition to be evaluated. You should not evaluate anything else if that is false, or you'll get the undefined index errors. Commented Feb 17, 2012 at 13:59
  • The image doesn't exist in $_FILES possibly because it failed to upload in the first place. Check if $_POST['event_image'] is set and verify you didn't exceed max_upload_size or post_max_size Commented Feb 17, 2012 at 14:00

3 Answers 3

1

The $_FILES array is numerically indexed under each property key, to deal with multiple uploads.

You have to hardcode index zero, using $_FILES["event_image"]["type"][0], $_FILES["event_image"]["name"][0] etc. You have to change every line where you are using $_FILESwithout the numeric indexes.

NOTICE: Original answer edited to fix the proper location of the numeric indexes within the array.

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

2 Comments

That's not true. $_FILES is an associated array with the name field mapped as key: us.php.net/manual/en/reserved.variables.files.php
You're right, I messed up! The numeric indexes for multiple uploads is deep inside that array. Answer edited considering the proper $_FILES structure.
1

OK... it would appear it was a folder permissions issue on my local machine... Just had to chmod the folder.

Thanks for all the advice though.

2 Comments

Can you please elaborate what exactly did you do with folder/file permissions to remove the error ?
In my case, I used Dreamweaver. I right clicked the directory in the files window and selected "Set permissions" where I was able to change the permissions to exactly what I needed to.
0

Just so you know how the $_FILES object look like, right before line number 8, insert this echo "<PRE>" . print_r ($_FILES, true) . "</PRE>";

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.