Reputation: 91
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();
}
?>
Upvotes: 0
Views: 211
Reputation: 74217
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:
Upvotes: 0
Reputation: 1153
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);
Upvotes: 1
Reputation: 55
Make sure you have enctype='multpart/form-data' in the form
When inserting to the database, make sure you insert the file path i.e '$target_dir/filetempname'
$imagepath = $target_dir/filetempname
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. }
We get file data using $_FILES variable not $_POST even though files are posted using post method.
Upvotes: 0