本文介绍了图像不使用路径从数据库中获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用路径从数据库上传和获取图像.上传过程完美.但是,我无法从数据库中获取图像.我试过 print_r($row['image']);
.我得到这样的路径 C:/xampp/htdocs/xampp/htdocs/www/images/0d13808ad672c2713d306efbb0e42918
.我不知道为什么这段代码没有从 db 中获取图像?
I'm trying to upload and fetch images from database using path. Upload process working perfectly. But, I cannot able to fetch image from db. I've tried print_r($row['image']);
. I'm getting the path like this C:/xampp/htdocs/xampp/htdocs/www/images/0d13808ad672c2713d306efbb0e42918
. I don't know why this code doesn't fetch image from db?
fetch.php
<?php
include('config.php');
ini_set('display_startup_errors',1); a
ini_set('display_errors',1);
error_reporting(-1);
try
{
$stmt = $conn->prepare("SELECT * FROM imgdb WHERE id = 3");
$conn->errorInfo();
// $stmt->bindParam('1', $imgid, PDO::PARAM_INT);
$stmt->execute();
// $path = "/xampp/htdocs/www/images/";
// $imgpath = $_SERVER['DOCUMENT_ROOT'].$path;
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<img src=".$row['image']." height='100' width='100'/>";
print_r($row['image']);
}
}
catch (PDOException $e)
{
echo 'Database Error'.$e->getMessage();
}
?>
上传.php
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
include('config.php');
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
// echo "Upload: " . $_FILES["file"]["name"] . "<br>";
// echo "Type: " . $_FILES["file"]["type"] . "<br>";
// echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
// echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$filename = basename($_FILES['file']['tmp_name']);
$newname = md5($filename);
$final_save_dir = '/xampp/htdocs/www/images/'.$newname ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $final_save_dir . $_FILES['file']['name']))
{
echo "Uploaded";
}
else
{
echo "File was not uploaded";
}
$imgid = $_SERVER['DOCUMENT_ROOT'].$final_save_dir;
try
{
$stmt = $conn->prepare("INSERT INTO imgdb ( image ) VALUES ( ? ) ");
$stmt->bindParam('1', $imgid, PDO::PARAM_STR);
$conn->errorInfo();
$stmt->execute();
}
catch (PDOException $e)
{
echo 'Database Error'.$e->getMessage();
}
?>
推荐答案
试试这个并和你的比较一下,我不知道它是否有效,因为我没有测试过,但它应该.
Try this and compare with yours, I don't know if it works because I didn't tested, but it should.
上传.php
include('config.php');
if ($_FILES["file"]["error"] > 0 )
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$filename = basename($_FILES['file']['tmp_name']);
$ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$new_file_name = md5($filename).'.'.$ext;
$final_save_dir = $_SERVER['DOCUMENT_ROOT'].DS.'www'.DS.'images'.DS;
if(move_uploaded_file($_FILES['file']['tmp_name'], $final_save_dir . $new_file_name))
{
echo "Uploaded";
}
else
{
echo "File was not uploaded";
}
try
{
$stmt = $conn->prepare("INSERT INTO imgdb ( image ) VALUES ( ? ) ");
$stmt->bindParam('1', $new_file_name, PDO::PARAM_STR);
$conn->errorInfo();
$stmt->execute();
}
catch (PDOException $e)
{
echo 'Database Error'.$e->getMessage();
}
}
fetch.php
<?php
include('config.php');
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
try
{
$stmt = $conn->prepare("SELECT * FROM imgdb WHERE id = 3");
$conn->errorInfo();
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<img src='images/".$row['image']."' height='100' width='100'/>";
}
}
catch (PDOException $e)
{
echo 'Database Error'.$e->getMessage();
}
这篇关于图像不使用路径从数据库中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!