Reputation: 992
I'm trying to upload an image using PHP to MySQL. In the query i'm trying to save the image and the directory of the image in the DB, but I get empty directory in the DB, plus no image in the folder. Any help would be greatly appreciated!
<?php
// include db connect class
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');
$db = new DB_CONNECT();
//Setting up images directory
$target = "./images";
$target = $target . basename( $_FILES['photo']['name']);
$photo=($_FILES['photo']['name']);
//inserting data order
$order = "INSERT INTO scb
(photo, directory)
VALUES
( '$_POST[$target]',
'({$_FILES['photo']['name']})')";
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
//declare in the order variable
$result = mysql_query($order); //order executes
if($result){
echo "Thank you for submitting!";
} else{
echo "Sorry, something went wrong! Please try again!";
}
?>
Upvotes: 1
Views: 110
Reputation: 477
Well first mysql_query
is outdated use PDO
instead (you'll have to check if this feature is enabled on your server by using phpinfo()
);
Try this
<?php
//Connect to sql db
try {
$user = "username";
$pass = "password";
$db = new PDO('mysql:host=yourhost;dbname=yourdb', $user, $pass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
//Setting up images directory
$target = "./images/"; //you forgot the last slash
$target = $target . basename($_FILES['photo']['name']);
$photo = $_FILES['photo']['name'];
//inserting data order
$stmt = $db->prepare("INSERT INTO scb (photo, directory) VALUES (:photo, :directory)");
$stmt->bindParam(':photo', $_POST[$target]);
$stmt->bindParam(':directory', $_FILES['photo']['name']);
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives an error if its not
echo "Sorry, there was a problem uploading your file.";
}
//declare in the order variable
if($stmt->execute()){
echo "Thank you for submitting!";
} else{
echo "Sorry, something went wrong! Please try again!";
}
?>
Upvotes: 1
Reputation: 92
Have you checked for any errors or warnings in the web server log files? Do you get any sensible output if you write:
print_r($_FILES);
Upvotes: 0