0

I am getting an error stating that fileImage is an undefined index on this line: $idx = count($_POST ['fileImage']) -1 ;. Now this is happening because when user opens up the page, then obviously nothing is posted to fileImage, so how can I set it to "", when nothing is posted to the $_POST? I thought I did it in the line below but it doesn't seem that is happening.

Below is the code:

<?php
session_start();

$idx = count($_POST ['fileImage']) -1 ;
$output = isset($_POST ['fileImage'][$idx]) ? $_POST ['fileImage'][$idx]['name'] : "";

?>

Javascript:

function stopImageUpload(success) {

    var imageNameArray = ['<?php echo $output ?>'];
    var result = '';

    if (success == 1) {
        result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
        for (var i = 0; i < imageNameArray.length; i++) {
            $('.listImage').append(imageNameArray[i] + '<br/>');
        }
    }
    else {
        result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
    }

    return true;
}​

Below is the php script where it uploads a file which is on another page from the javascript function above:

        <?php

            session_start();

            $result = 0;
            $errors = array ();
            $dirImage = "ImageFiles/";


        if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {

        $fileName = $_FILES ['fileImage'] ['name'];

        $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExt = strtolower ( $fileExt );


        $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;

                if (count ( $errors ) == 0) {
                    if (move_uploaded_file ( $fileTemp, $fileDst )) {
                        $result = 1;


                    }
                }

            }

    $_POST ['fileImage'][] = array('name' => $_FILES ['fileImage']['name']);

            ?>

        <script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result;?>);</script>
user1359453
  • 75
  • 1
  • 7

3 Answers3

2

I don't see where you are checking for that but the way to do it is:

if(!isset($_POST['fileImage'))
      // Don't do something with the image
else
      // Totally do it
Zombaya
  • 2,230
  • 23
  • 30
  • I did it on this line below the line where the error is: `$output = isset($_POST ['fileImage'][$idx]) ? $_POST ['fileImage'][$idx]['name'] : "";` – user1359453 Apr 28 '12 at 22:48
2

For uploaded files please refer to $_FILES instead of $_POST.

http://php.net/manual/en/reserved.variables.files.php

Zombaya
  • 2,230
  • 23
  • 30
Stefan Dochow
  • 1,454
  • 1
  • 10
  • 11
  • I do refer to $_FILES, it is just that I use $_POST to retrieve the $_FILES from the php script which does the uploading which is on a seperate page. I also posted this code so you can see :). – user1359453 Apr 28 '12 at 22:39
0

If you don't want to write a log isset all the time you can use this function ...

function __R($var) {
    return isset ( $_REQUEST [$var] ) ? $_REQUEST [$var] : null;
}

$idx = count ( __R ( "fileImage" ) ) - 1;
Baba
  • 94,024
  • 28
  • 166
  • 217