gpt4 book ai didi

php - 使用 PHP 将图像上传到 MySQL

转载 作者:行者123 更新时间:2023-11-29 00:07:50 26 4
gpt4 key购买 nike

我正在尝试使用 PHP 将图像上传到 MySQL。在查询中,我试图将图像和图像的目录保存在数据库中,但我在数据库中得到空目录,而且文件夹中没有图像。任何帮助将不胜感激!

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

最佳答案

好先mysql_query已过时使用PDO相反(您必须使用 phpinfo() 检查您的服务器上是否启用了此功能);

试试这个

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

关于php - 使用 PHP 将图像上传到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com