-1

This is my HTML:

<!DOCTYPE html>
<html>
    <body>
        <form action="uploaded_img.php" method="post" enctype="multipart/form- 
         data">
            Select image to upload:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit">
        </form>

    </body>
</html>

PHP code:

<?php 
 if(isset($_POST['submit'])){
    $name = $_FILES['file']['name'];  
    $temp_name = $_FILES['file']['tmp_name'];  
    if(isset($name)){
        if(!empty($name)){      
            $location = 'uploads/';      
            if(move_uploaded_file($temp_name, $location.$name)){
                echo 'File uploaded successfully';
            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
}
?>

My code is giving me these errors:

Notice: Undefined index: file in C:\xampp\htdocs\phptutorial\uploaded_img.php on line 3 Notice: Undefined index: file in C:\xampp\htdocs\phptutorial\uploaded_img.php on line 4 You should select a file to upload !!

i want to save the upload image in c:xamp\htdocs\phptutorials\uploads

Patrick Q
  • 6,373
  • 2
  • 25
  • 34

2 Answers2

0

Add a right name of file in your code. Like this

<?php 
 if(isset($_POST['submit'])){
    $name = $_FILES['fileToUpload']['name'];  
    $temp_name = $_FILES['fileToUpload']['tmp_name'];  
    if(isset($name)){
        if(!empty($name)){      
            $location = 'uploads/';      
            if(move_uploaded_file($temp_name, $location.$name)){
                echo 'File uploaded successfully';
            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
}
?>
Gaurav
  • 442
  • 3
  • 7
0

Try this

if(isset($_POST['submit']))    {
    if(!empty($_FILES['fileToUpload']))
          {
            $path = "uploads/";
            $path = $path . basename( $_FILES['fileToUpload']['name']);
            if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $path)) {
              echo "The file ".  basename( $_FILES['fileToUpload']['name']). 
              " has been uploaded";
            } else{
                echo "There was an error uploading the file, please try again!";
            }
          }
    }
jvk
  • 2,133
  • 3
  • 19
  • 28