0
  1. All is going fine but got this error
    Notice: Undefined index: image in C:\wamp\www\neos-final\update-image.php on line 21
  2. Line 21 is $image=$_FILES['image']['name'];
  3. What to do with this line :(

    <form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="image" id="image" size="40">
    <input name="" type="submit" value="upload" />
    
    </form>
    <?php 
    $con = mysql_connect('localhost', '', ''); //Update hostname
    mysql_select_db("test", $con); //Update database name
    
    define ("MAX_SIZE","1000"); 
    function getExtension($str)
    {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
    }
    
    $errors=0;
    $image=$_FILES['image']['name'];
    if ($image) 
    {
        $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
        if ( ($extension != "jpg") && 
             ($extension != "jpeg") && 
             ($extension != "png") && 
             ($extension != "gif") && 
             ($extension != "JPG") && 
             ($extension != "JPEG") && 
             ($extension != "PNG") && 
             ($extension != "GIF") ) 
        {
            echo '<h3>Unknown extension!</h3>';
            $errors=1;
        }
        else
        {
            $size=filesize($_FILES['image']['tmp_name']);
    
            if ($size > MAX_SIZE*1024)
            {
                echo '<h4>You have exceeded the size limit!</h4>';
                $errors=1;
            }
    
            $image_name=time().'.'.$extension;
            $newname="images/".$image_name;
    
            $copied = copy($_FILES['image']['tmp_name'], $newname);
            if (!$copied) 
            {
                echo '<h3>Copy unsuccessfull!</h3>';
                $errors=1;
            }
            else echo '<h3>uploaded successfull!</h3>';
    
            mysql_query("insert into list (image) values('".$newname."')");
        }
    
        //Display image
        $rs=mysql_query("select * from list");
        if($rs)
            while($row=mysql_fetch_array($rs))
            {
             ?>
             <img width="150" src="<?php echo $row['image'];?>"><br>
             <?php 
            }
    }
    ?>
    

Any idea?

2
  • You can Check DEveloper Tool to check what is being Posted on submit as it is because of undefined Veraible? Commented Apr 27, 2013 at 6:24
  • 1
    Why do you test $extension for uppercase strings when strtolower is applied? Commented Apr 27, 2013 at 6:30

2 Answers 2

1

This notice Notice: Undefined index: comes when any index of array variable is not defined.

use isset here

if(isset($_FILES['image']))
{
    $image=$_FILES['image']['name'];
    //your rest of code here
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sir one more thing is...when I upload any image here it creates an id automatically say id = 0 in database and upload image..whereas I want the existing image to be replace with the new one?...can u pls check my code for this
@SwatiKanojia: If your updating the existing image then you need to pass id of the existing image as hidden value with form,by checking id you can update the image instead of creating new record.
Is there any simple code to do that...all these code sucks my mind :( dont knw what to do
0
Use php isset()

if(isset($_FILES['image']))
{
  // your code 
}

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.