image does not fetch from database using path(图像不使用路径从数据库中获取)
问题描述
我正在尝试使用路径从数据库上传和获取图像.上传过程完美.但是,我无法从数据库中获取图像.我试过 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();
}
这篇关于图像不使用路径从数据库中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:图像不使用路径从数据库中获取
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01