0

I am getting the following errors:

Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 5

Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 7

Here is the code:

<?php

$target_path = "images/";

$target_path = $target_path . basename( $_FILES['theimage']['name']);

if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ".  basename( $_FILES['theimage']['name']). " has been uploaded</p>";
} else{
echo "<p>There was an error uploading the image, please try again!</p>";
}  

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>

<form enctype="multipart/form-data" action="index.php" method="POST">
<fieldset>
<input name="theimage" type="file" />
<input type="submit" value="Upload" />
</fieldset>
</form>



</body>
</html>

Any ideas on how to fix this please ?

2
  • What does print_r($_FILES) give you ? Commented Aug 16, 2011 at 20:36
  • Can you post the var_dump or print_r of the $_FILES array? Also, what are the values for these three php settings? file_uploads, upload_max_filesize, max_file_uploads Commented Aug 16, 2011 at 20:36

3 Answers 3

2
<?php
if(!empty($_FILES['theimage'])) {
    // YOUR CURRENT PHP CODE HERE
?>

The problem is the upload script is executing even if nothing is posted (like when you first load the page).

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

Comments

2

Check to make sure you're getting data in $_FILES or $_POST before you try to do anything with them:

<?php
    if(isset($_FILES)) {        // if(isset($_POST)) 
                                // would work as well
        $target_path = "images/";
        $target_path = $target_path . basename( $_FILES['theimage']['name']);
        if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
            echo "<p>The image ".  basename( $_FILES['theimage']['name']). 
                " has been uploaded</p>";
        } else {
            echo "<p>There was an error uploading the image!</p>";
        } 
    } 
?>

Comments

0

Change your submit button to:

<input type = 'submit' name = 'theSubmitBtn' value = 'Upload'>

Then, wrap all of your php code at the top inside of this if check:

if(isset($_POST['theSubmitBtn'])) {

... Your Code ...

}

See if that works for you. On a side note, you will also need to add much more error checking if this is to be used in a production environment.

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.