user4514748
user4514748

Reputation:

Error : Undefined index - PHP, MySql

I am inserting profile image in folder and in database table, image is well uploaded to the "uploads" folder, but not inserting in mysql database table. I am getting this error:

Notice: Undefined index: fileToUpload in C:\wamp\www\project\upload_image.php on line 10

From the following code:

upload_image.php

<?php
include_once('dbconn.php');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $user_id = $_POST['user_id'];
  $fileToUpload = $_POST['fileToUpload'];
  $sql="INSERT INTO profile_image (user_id,fileToUpload) VALUES ('$user_id', '$fileToUpload')";
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } 
  else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
    if (file_exists($target_file)) {
      echo "Sorry, file already exists.";
      $uploadOk = 0;
    }

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
} 
else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))     {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  } 
  else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

HTML code

<form method="post" action="upload_image.php" enctype="multipart/form-data">
  <?php if(isset($_SESSION['email'])){?>
  <input type="hidden" name="user_id" value="<?php echo ($_SESSION['id']);}else{header("Location: login.php");}?>">
  <input type="file" name="fileToUpload">
  <input type="submit" value="Upload" name="submit">
</form>

Does anyone know how to fix this?

Upvotes: 2

Views: 10164

Answers (3)

waroa kawa
waroa kawa

Reputation: 29

The following code is an another way to solve your problem:

<?php
$target_dir = "uploads/";
$uploadOk = 1;

if(isset($_POST["submit"])||!empty($_FILES["fileToUpload"]["name"])) {
    $target_file =$target_dir.basename($_FILES["fileToUpload"]["name"]);
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $check["mime"];
        $uploadOk = 1;
        if (file_exists($target_file)) {
            echo "File already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }

            } else {
       if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
            echo "Only JPG, JPEG & PNG files are allowed.";
            $uploadOk = 0;
        }
        $uploadOk = 0;
    }

    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
}}
?>

Upvotes: 0

Joel Davis
Joel Davis

Reputation: 552

The fileToUpload variable does not exist as a $_POST

 $_POST['fileToUpload'];

If has to be set as $_FILE as was previously used

 $_FILE['fileToUpload'];

However $_FILE[''] array is broken up into different components and you need to select the part of the array you are after.

For example if you were trying to save the name of the file to the database use

$_FILES["fileToUpload"]["tmp_name"];

Upvotes: 1

Muhammad Ahmed
Muhammad Ahmed

Reputation: 481

$fileToUpload = $_FILES['fileToUpload']['name'];

Try this.

Upvotes: 1

Related Questions