2

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?

10
  • Show your profile_image table structure Commented Apr 10, 2015 at 5:25
  • Where is line no 10 in your code? Commented Apr 10, 2015 at 5:28
  • use $_FILES instead of $fileToUpload = $_POST['fileToUpload']; Commented Apr 10, 2015 at 5:29
  • $fileToUpload = $_POST['fileToUpload']; you trying to get the file upload as $_POST try $_FILES Commented Apr 10, 2015 at 5:29
  • @Raja Now getting this error Array to string conversion in C:\wamp\www\project\upload_image.php on line 11 This is my line number 11 $sql="INSERT INTO profile_image (user_id,fileToUpload) VALUES ('$user_id', '$fileToUpload')"; Commented Apr 10, 2015 at 5:33

3 Answers 3

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

Try this.

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

12 Comments

Error removed but image name is not inserting in database.
Array to string conversion in C:\wamp\www\project\upload_image.php on line 11 This is my line number 11 $sql="INSERT INTO profile_image (user_id,fileToUpload) VALUES ('$user_id', '$fileToUpload')";
On line number 10 use this. $fileToUpload = $_FILES['fileToUpload']['name'];
I just echo the sql query check this out INSERT INTO profile_image (user_id, fileToUpload) VALUES ('1', 'AIS14E.JPG') but not inserting in database.
because you are not executing this query.
|
1

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"];

Comments

0

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.";
        }
}}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.