0

I'm trying to upload image files to my local folder (local hosting). I manage to do it. The problem is, I don't see the image path in the sql table including the rest of the data that suppose to be inserted in the table. Previously I was able to, before I messed around. I previously made minor changes to upload.php. Even though I manage to upload image files to the local folder (the same directory as my pages). I've tried this since morning and now it's midnight. I also get an error "Undefined index: image..line 106" -->>

$image = $_POST['image'];

Please help.tq

Below is the upload.php.

   <?php


//echo var_dump($_POST);
//echo var_dump($_FILES);

session_start();



require 'connect-test.php';




if(isset($_POST["submit"])) {




    $id = $_POST['id'];
    $name2 = $_POST['name2'];
    $color2 = $_POST['color2'];
    $hobby2 = $_POST['hobby2'];
    $radiobtn = $_POST['radiobtn'];
    $image = $_FILE['image'];







        $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image or not    


    $check = getimagesize($_FILES["image"]["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["image"]["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["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}





    $stmt = $conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2,radiobtn,image) VALUES (?,?,?,?,?,?)");
    $stmt->bind_param("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file);
    $stmt->execute();


}




?>
  • and you already posted a similar question earlier http://stackoverflow.com/q/34567413/ about the same problem. – Funk Forty Niner Jan 02 '16 at 17:48
  • @Fred. Previously it was about undefined index. BUt now i manage to insert the image to the folder, except the path. but in the same time, i'm receiving some errors also. it's a 2 in 1 problem now. – ml2_1jzsinglecam Jan 02 '16 at 18:01
  • After one problem solved previously, a new problem is created but comes with another one. it's a bit complicated – ml2_1jzsinglecam Jan 02 '16 at 18:02
  • ok, well see the other answers given below. I've posted one, but that may not be enough to completely solve the problem you are now having. – Funk Forty Niner Jan 02 '16 at 18:04
  • if you're trying to save the path of the file, then you're using the wrong variable. In `("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$image);` you need to use `("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file);` as per `$target_file = $target_dir . basename($_FILES["image"]["name"]);` and is what Webster wrote in their answer. But you will still need to change this `$image = $_POST['image'];` as per what I said `$image = $_FILES['image'];` – Funk Forty Niner Jan 02 '16 at 18:12
  • tq u to all for the reply. i still got an error Notice: Undefined variable: _FILE in C:\xampp\htdocs\eventsite\upload.php on line 26 – ml2_1jzsinglecam Jan 02 '16 at 18:19
  • `_FILE`? you forgot the S here. – Funk Forty Niner Jan 02 '16 at 18:20
  • I managed to save the filepath aready in the sql table. tq. and also in the image folder. but this undefined error keeps prompting – ml2_1jzsinglecam Jan 02 '16 at 18:20
  • i will update my new code above. – ml2_1jzsinglecam Jan 02 '16 at 18:22
  • look at my answer http://stackoverflow.com/a/34568860/1415724, I wrote 35 mins prior to this comment, it's `$_FILES` with an `S`, and not `$_FILE`. `$image = $_FILE['image'];` to `$image = $_FILES['image'];` and in my comment above too. That's why it's giving you that error. – Funk Forty Niner Jan 02 '16 at 18:23
  • OMG. Silly me. sorry @Free-ii-. Tq so much. And also to the rest. Tq for all the correct answers. – ml2_1jzsinglecam Jan 02 '16 at 18:27
  • you're welcome, glad it worked out, *cheers* – Funk Forty Niner Jan 02 '16 at 18:27

3 Answers3

1

You want to insert the image's path right? This variable already return the image's path

$target_file = $target_dir . basename($_FILES["image"]["name"]);

Then change this

$stmt->bind_param("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$image);

to this

$stmt->bind_param("isssss",$id,$name2,$color2,$hobby2,$radiobtn,$target_file);
Webster
  • 1,113
  • 2
  • 19
  • 39
0

As per your original post https://stackoverflow.com/revisions/34568743/1

We're dealing with $_FILES here and not $_POST.

Therefore you need to change your $_POST to $_FILES for $image = $_POST['image'];

Reference:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0
  1. Make sure you have enctype='multpart/form-data' in the form

  2. When inserting to the database, make sure you insert the file path i.e '$target_dir/filetempname'

    $imagepath = $target_dir/filetempname

  3. Please insert in db only if move_uploaded is successful. Otherwise things will be empty nad security reason.

    if(move_uploaded_file is successful){ //code to insert in db. }

  4. We get file data using $_FILES variable not $_POST even though files are posted using post method.

Kim
  • 55
  • 8