-1

I'm only a beginner with PHP and server side programming so excuse me for this question about variables. I was reading a tutorial about uploading a photo.

(tutorial on plus2net) and like described in the tutorial i directly copied and pasted the code after understanding it. However the webpage filled with following errors after i inserted PHP in to the HTML.

Notice: Use of undefined constant size - assumed 'size' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44

Notice: Undefined index: file_up in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44

Notice: Use of undefined constant file_up - assumed 'file_up' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 45

Notice: Use of undefined constant name - assumed 'name' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 45

Notice: Undefined index: file_up in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 45

Notice: Use of undefined constant file_up - assumed 'file_up' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 47

Notice: Use of undefined constant size - assumed 'size' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 47

Notice: Undefined index: file_up in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 47

Notice: Use of undefined constant file_up - assumed 'file_up' in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 51

This is the PHP Code:

<?Php
    $file_upload="true";
    $file_up_size=$_FILES['file_up'][size];
    echo $_FILES[file_up][name];

    if ($_FILES[file_up][size]>500000){
    $msg=$msg."Uploaded file size more than 500KB<BR>";
    $file_upload="false";}

    if (!($_FILES[file_up][type] =="image/jpeg" OR $_FILES[file_up][type] =="image/png")){
    $msg=$msg."Your uploaded file must be of JPG or PNG.<BR>";
    $file_upload="false";}

    $file_name=$_FILES[file_up][name];
    $add="upload/$file_name"; //the path with the file name

    if($file_upload=="true"){
        if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
        //do your coding here to give a thanks message

        }
        else{
            echo "Failed to upload file.";}
            }
    else{
        echo $msg;
        }
    ?>

I've corrected the variable error but I'm getting this error now instead

Notice: Undefined index: file_up in C:\Program Files (x86)\XAMPP\htdocs\college_ink\shirtDesign.php on line 44

Please guide me through this. :)

Ayub Bukhari
  • 51
  • 1
  • 8
  • 1
    You should be quoting the array keys: `$_FILES['file_up']['size']` Otherwise, PHP is expecting a constant. – showdev Aug 25 '14 at 19:01
  • Think "strings", not "constants" ;) – Funk Forty Niner Aug 25 '14 at 19:08
  • [That tutorial](http://www.plus2net.com/php_tutorial/php_file_upload.php) you followed, should either be modified or deleted altogether. This `['file_up']` is a [string](http://php.net/manual/en/language.types.string.php); while this `[file_up]` is a [constant](http://php.net/manual/en/language.constants.php). – Funk Forty Niner Aug 25 '14 at 19:09

2 Answers2

1

Where you have

$file_up_size=$_FILES['file_up'][size];

size is being interpreted as a constant due to the lack of a dollar sign.

What you want is

$file_up_size=$_FILES['file_up']['size'];
castis
  • 8,154
  • 4
  • 41
  • 63
0

The problem is that you are relying on bare strings being interpreted as such. Normally, in PHP code, this is considered a constant:

echo FOO;

If you don't define that constant first, like this:

define('FOO', 'foo');

You will encounter this error and PHP will interpret it as a bare string and echo FOO. This is a bad choice. I think what you mean to do is use actual strings (rather than undefined constants) as your array indices:

if ($_FILES['file_up']['size']>500000){
    // etc..

Which is what is recommended.

Note: Bare strings are covered in the manual entry for Arrays, specifically underneath the section Arrays do's and don'ts (the manual says don't do this).

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • thanks it worked. But instead of undefined variable error i'm now getting undefined index error! any suggestions? – Ayub Bukhari Aug 25 '14 at 19:09
  • It looks like you may be trying to upload a file larger than the php.ini setting for `upload_max_filesize` or `post_max_size`. If the file you are trying to upload is too large, then the index into the `$_FILES` array won't even be created. The constant you are checking against (500000) doesn't come into play in such cases. See [this question](http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php) – Jeff Lambert Aug 25 '14 at 19:24