0

I have the following code that is being used to successfully upload images:

$fileName = $_FILES['file']['name'];
$fileSize =  $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fileTmp = $_FILES['file']['tmp_name'];
list($origWidth, $origHeight) = getimagesize($fileTmp);

The catch is I'm getting an 'Undefined index' on each line.

I have print_r($_FILES) just before this code and each array variable exists and I've echo out varibles like $fileName and can see each has a value.

Also I have put this before this code block and it makes no difference:

$fileName = $fileSize = $fileType = $fileTmp = ''; 

Any suggestions how I can ensure these variables are defined?

thankyou

update.............................

this is the HTML code:

      <form action="http://www.domainname.com/scripts/php/processing.php?page=join&section=precrop&type=profile&token=photoToken" method="post" id="joinPhotoUploadFormProfile" enctype="multipart/form-data">
        <input type="file" name="file" class="fileProfile"><br>
      </form>

update...............................

var_dump($_FILES); output below:

enter image description here

5
  • Undefined index occurs when you try to access an array index - $_FILES['file'], $_FILES['file']['tmp_name'], etc. When does this warning occur? You can use isset(), ie isset($_FILES['file']){...). Commented Jun 30, 2013 at 4:26
  • please post the HTML code Commented Jun 30, 2013 at 4:28
  • What does the var_dump($_FILES) say? Commented Jun 30, 2013 at 4:30
  • I've added var_dump($_FILES) output now - cheers Commented Jun 30, 2013 at 4:34
  • 1
    Ok - I think we might be barking up the wrong tree here. If the script is successfully uploading files then the undefined index isn't in this section of code, I'd guess. Please post the full text of the error you're getting, and working from the line number in that message, post the affected section of code. Commented Jun 30, 2013 at 4:47

1 Answer 1

1

Since you are getting Undefined index and the script completes the upload successfully just add the following lines to make sure that the request method is in fact POST and that the $_FILES array is set.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fileName = (isset($_FILES['file']['name']) ? $_FILES['file']['name'] : null);
    $fileSize = (isset($_FILES['file']['size']) ? $_FILES['file']['size'] : null);
    $fileType = (isset($_FILES['file']['type']) ? $_FILES['file']['type'] : null);
    $fileTmp = (isset($_FILES['file']['tmp_name']) ? $_FILES['file']['tmp_name'] : null);

    //the rest of your upload code
}

it also looks like you are passing a lot of variables via url processing.php?page=join&section=precrop&type=profile&token=photoToken whick would suggest that you have other conditionals in the script so make sure that your are editing the code in the right place.

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

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.