【发布时间】:2016-05-16 01:41:29
【问题描述】:
我对 PHP 非常陌生,需要为一个项目解决这个问题 - 我在 HTML 中制作了一个图像提交表单,它将 div 中的 img 更改为使用该表单选择的图像。这是我如何做到这一点的:
echo '<div class="img-container">';
echo '<img class="userimg" src="../images/backgroundplanet.png" />';
echo '<img class="testimg" src="" />'; //stores image from php file
echo '</div>';
echo '<div class="upload-button">Edit Profile</div>';
echo '<input class="file-upload" enctype="multipart/form-data" type="file" name="submit" accept="image/*"/>';
echo '</div>';
和
$(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.userimg').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(".file-upload").on('change', function(){
readURL(this);
});
$(".upload-button").on('click', function() {
$(".file-upload").click();
});
});
我需要将此图像上传到我为 MySql 数据库中为每个用户创建的中等 BLOB 的列中。我尝试了许多不同的方法(尝试过$sql = new mysql 等),但不知道我在做什么。
我现在正在学习本教程 - http://www.sevenkb.com/php/how-to-insert-upload-image-into-mysql-database-using-php-and-how-to-display-an-image-in-php-from-mysql-database/ 并编写了以下内容,应该将图像上传到数据库:
if(!isset($_FILES['upload_image']))
{
echo '<p>Please Select Image to Upload</p>';
}
else
{
try {
upload();
echo '<p>Image Uploaded into MySQL Database as LONGBLOB Using PHP </p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload(){
/*** check if a file was uploaded ***/
echo '<p>you uploaded</p>';
if(is_uploaded_file($_FILES['upload_image']['tmp_name']) && getimagesize($_FILES['upload_image']['tmp_name']) != false)
{
/*** get the image info. ***/
$size = getimagesize($_FILES['upload_image']['tmp_name']);
/*** assign our variables ***/
$type = $size['mime'];
$imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['upload_image']['name'];
$maxsize = 99999999;
/*** check the file is less than the maximum file size ***/
if($_FILES['upload_image']['size'] < $maxsize )
{
/*** connect to db ***/
$dbh = new PDO("mysql:host=localhost;dbname=sqlserver", 'username', 'password');
/*** set the error mode ***/
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** our sql query ***/
$stmt = $dbh->prepare("INSERT INTO img (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");
/*** bind the params ***/
$stmt->bindParam(1, $type);
$stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $name);
/*** execute the query ***/
$stmt->execute();
}
else
{
/*** throw an exception is image is not of type ***/
throw new Exception("File Size Error");
}
}
else
{
// if the file is not less than the maximum allowed, print an error
throw new Exception("Unsupported Image Format!");
}
}
但是图片没有上传,也没有出现在我在数据库中创建的新列中。页面上唯一显示的是“请选择要上传的图片”
这里有什么问题?最终我需要在 div 中回显这个。我应该怎么做?
尝试运行示例时:
输入我的表名时出错:
【问题讨论】:
-
不建议将图片直接上传到数据库表中。您说对该图像的引用作为链接。这是一种更清洁的方式。另外,从您的 javascript 中,您是否尝试通过 Ajax 上传,因为如果那是您想要的,我认为根本不需要 js,它可以仅使用 php 完成
-
不,这就是我如何让我的表单首先更改 div 中的图像。我正在寻找将图像的引用作为链接上传并使用 php 将其回显到 div 的最简单方法。我对此很陌生-我将如何做到这一点?