如何使用php在mysql数据库中存储图像 [英] How to store images in mysql database using php

查看:149
本文介绍了如何使用php在mysql数据库中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在MySQL数据库中存储和显示图像。直到现在,我只写了代码来获取用户的图像并将它们存储在一个文件夹中,我写到现在的代码是:
HTML文件

 < input type =filename =imageUploadid =imageUpload> 

PHP文件

  $ target_dir =uploads /; 
$ target_file = $ target_dir。基名($ _ FILES [ imageUpload] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);


if(move_uploaded_file($ _ FILES [imageUpload] [tmp_name],$ target_file)){
echoThe file。 basename($ _FILES [imageUpload] [name])。 已上传。;
} else {
echo抱歉,上传文件时出现错误。;}


我找到了答案,对于那些在这里寻找同样的东西的人来说,我是这么做的。
您不应该考虑将图像上传到数据库,而是可以将上传文件的名称存储在数据库中,然后检索文件名并在任何想要显示图像的地方使用。



HTML代码

 < input type =file name =imageUploadid =imageUpload> 

PHP代码

  if(isset($ _ POST ['submit'])){

//处理用户上传的图片

$ target_dir =uploads /;
$ target_file = $ target_dir。基名($ _ FILES [ imageUpload] [ 名称]);
$ uploadOk = 1;
$ imageFileType = pathinfo($ target_file,PATHINFO_EXTENSION);

if(move_uploaded_file($ _ FILES [imageUpload] [tmp_name],$ target_file)){
echoThe file。 basename($ _FILES [imageUpload] [name])。 已上传。;
} else {
echo抱歉,上传文件时发生错误。;
}

$ image = basename($ _FILES [imageUpload] [name],。jpg); //用于将文件名存储在变量中

//将数据存入数据库
$ query =INSERT INTO items VALUES('$ id','$ title',' $描述, '$价格', '$值', '$接触', '$图像');
mysql_query($ query);

require('heading.php');
回声您的添加已被提交,您将在3秒内重定向到您的帐户页面....;
header(Refresh:3; url = account.php,true,303);
}

显示图像的代码

  while($ row = mysql_fetch_row($ result)){
echo< tr>;
echo< td>< img src ='uploads / $ row [6] .jpg'height ='150px'width ='300px'>< / td>;
echo< / tr> \\\
;
}


How can i store and display the images in a MySQL database. Till now i have only written the code to get the images from the user and store them in a folder, the code that i wrote till now is: HTML FILE

<input type="file" name="imageUpload" id="imageUpload">

PHP FILE

    $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";}

解决方案

I found the answer, For those who are looking for the same thing here is how I did it. You should not consider uploading images to the database instead you can store the name of the uploaded file in your database and then retrieve the file name and use it where ever you want to display the image.

HTML CODE

<input type="file" name="imageUpload" id="imageUpload">

PHP CODE

if(isset($_POST['submit'])) {

    //Process the image that is uploaded by the user

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }

    $image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable

    //storind the data in your database
    $query= "INSERT INTO items VALUES ('$id','$title','$description','$price','$value','$contact','$image')";
    mysql_query($query);

    require('heading.php');
    echo "Your add has been submited, you will be redirected to your account page in 3 seconds....";
    header( "Refresh:3; url=account.php", true, 303);
}

CODE TO DISPLAY THE IMAGE

while($row = mysql_fetch_row($result)) {
    echo "<tr>";
    echo "<td><img src='uploads/$row[6].jpg' height='150px' width='300px'></td>";
    echo "</tr>\n";
}

这篇关于如何使用php在mysql数据库中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆