0

Hi can someone help me out, please. I have been using the same file upload script on three websites that I manage, with great success. But recently I have encountered some errors when accessing the file upload form on my locahost server on my own computer server. This has never happened before.

The following errors appear:
Notice: Undefined index: uploaded in C:\xampp\htdocs***************\upload_file.php on line 16
**Notice:
Undefined variable: uploaded_size in C:\xampp\htdocs*****************\upload_file.php on line 20
Notice: Undefined variable: uploaded_type in C:\xampp\htdocs*****************\upload_file.php on line 25
Notice: Undefined index: uploaded in C:\xampp\htdocs*****************\upload_file.php on line 35

These errors do not appear on the live websites, but only on my home server. I can still upload a file successfully while receiving these errors on my home computer.

I have tried to fix this problem up, but with no success.

Can someone help me out please?

File Upload Script:

<?php 
                $target = "uploads/"; 
                $target = $target . basename( $_FILES['uploaded']['name']) ; 
                $ok=1; 

                //This is our size condition 
                if ($uploaded_size >2950000){ 
                    echo "Your file is too large.<br>"; 
                    $ok=0; 
                } 
                //This is our limit file type condition 
                if ($uploaded_type =="text/php"){ 
                    echo "No PHP files<br>"; 
                    $ok=0; 
                } 
                //Here we check that $ok was not set to 0 by an error 
                if ($ok==0){ 
                    echo "Sorry your file was not uploaded"; 
                } 
                //If everything is ok we try to upload it 
                else { 
                    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
                    echo "
                          The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
                    } 
                    // else { 
                    //    echo "Sorry, there was a problem uploading your file."; 
                    //} 
                } 
            ?>
            </div>   
                <form enctype="multipart/form-data" action="upload_file.php" method="POST">
                    <fieldset>
                        <legend>&#160;Upload File&#160;|&#160;<a href="viewFile.php" class="apageLinks">View File List</a>&#160;|&#160;
                                                              <a href="menu.php" class="apageLinks">Main Menu</a>&#160;|&#160;
                                                              <a href="logout.php" class="apageLinks">Log out</a>&#160;
                        </legend><br /><br />
                        Please choose a file:<input name="uploaded" type="file" /><br /><br /> 
                        <input name="submit" type="submit" value="Submit" class="form" /><br /> 
                    </fieldset>       
                </form><br />

Edited by davBro: Moved.

2
Contributors
3
Replies
15
Views
5 Years
Discussion Span
Last Post by davBro
0

You get these notices because most likely, they are set to be hidden on your production server. You use some variables that may not be available/have a value. The use of isset() can solve this.

0

Hi pritaeas,

Thanks for you reply. As it it after 12midnight here, I'll have a look at it tommorrow, post an update.
Thanks again.

0

Hi Pritaeus,

I have used your suggestion with the addition of and '@' symbol before variables concerned in the code:- $upload_size, $uploaded_type, $_Files. I inserted the 'if isset' statement at the start of the code and ended it with a curly bracket at the end of the code. I am not sure if it is the correct thing to do but it seems to work. It has eliminated the errors and I now have a successful file upload with no errors. You can see these changes in the code I have inserted below:

Again, thanks for your help.

<?php
                if (isset($_POST['submit'])) { // Check if the form has been submitted.
                    $target = "uploads/"; 
                    $target = $target . basename( $_FILES['uploaded']['name']) ; 
                    $ok=1; 
                    //This is our size condition 
                    if (@$uploaded_size >2950000){ 
                        echo "Your file is too large.<br>"; 
                        $ok=0; 
                    } 
                    //This is our limit file type condition 
                    if (@$uploaded_type =="text/php"){ 
                        echo "No PHP files<br>"; 
                        $ok=0; 
                    } 
                    //Here we check that $ok was not set to 0 by an error 
                    if ($ok==0){ 
                        echo "Sorry your file was not uploaded"; 
                    } 
                    //If everything is ok we try to upload it 
                    else { 
                        if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
                        echo "              

                              The file ". basename(@$_FILES['uploadedfile']['name']). " has been uploaded"; 
                        } 
                        // else { 
                        //    echo "Sorry, there was a problem uploading your file."; 
                        //} 
                    } 
                }
                ?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.