php 使用php将图片插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22236035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert picture into database using php
提问by Marooweb
hello i have a form with many input ( name, last name, picture,age..) and i want to submit all this information into my database , i know how to do it for a simple input but with the picture i have some problem can someone help me please my html file is
你好,我有一个包含很多输入的表单(姓名、姓氏、图片、年龄..),我想将所有这些信息提交到我的数据库中,我知道如何进行简单的输入,但是对于图片我有一些问题有人可以帮我吗,我的 html 文件是
<form action="insertion.php" method="post" >
<table >
<tr>
<td><strong>Titre</strong></td>
<td><input name="titre" type="text" value="" /></td>
</tr>
<tr>
<td><strong>Annee</strong></td>
<td><input name="annee" type="number" value="" /></td>
</tr>
<tr>
<td><strong>Genre musical</strong></td>
<td><input name="Gmusical" type="texte" value="" /></td>
</tr>
<tr>
<td>
<strong>Picture</strong>
</td>
<td>
<input type="file" name="img"/>
</td>
</tr>
</table>
<input type="submit" value="Submit " />
</form>
my file insertion.php to sublmit into the database is
我要提交到数据库中的文件 insert.php 是
<?php
include("connexion.php");
$titre=$_POST['titre'];
$annee=$_POST['annee'];
$Gmusical=$_POST['Gmusical'];
$picture=$_POST['img'];
$req="INSERT INTO `cd`
(`titre`, `Annee`, `genremusical`, `Image`)
VALUES
('$titre','$annee','$Gmusical','$picture');";
if (mysql_query($req))
{
echo "ok";
}
else
echo 'ko';
}
回答by Waragi
Generally you do not want to store actual BLOB(binary large object) data types in a database. You store the path to the image, located somewhere on your web server.
通常,您不想在数据库中存储实际的 BLOB(二进制大对象)数据类型。您将图像的路径存储在 Web 服务器上的某个位置。
So in the column "Image", you would store the path "images/photo1103.jpg".
因此,在“Image”列中,您将存储路径“images/photo1103.jpg”。
To display the photo:
echo "<img src=". $image_query_fetch['Image'] .'" alt=\"\" />";
要显示照片:
echo "<img src=". $image_query_fetch['Image'] .'" alt=\"\" />";
回答by SuperDJ
the column type can be a varchar I would also suggest using mysqli instead of mysql
列类型可以是 varchar 我还建议使用 mysqli 而不是 mysql
and the following code should help you:
Edited the codetry this as one file and maybe try to debug it by using echo $picture_name;if you haven't tried this yet.
并且以下代码应该可以帮助您:
编辑代码尝试将此作为一个文件,echo $picture_name;如果您还没有尝试过,可以尝试使用调试它。
(Optional:One more thing that could help is ob_start();place this at the very top of the file just after the <?phptag and than at the bottom of the page close it with ob_flush();before ?>tag so you get something like:
)
(可选:还有一两件事,可以帮助ob_start();地方这个在最高层的文件只是后<?php标签,比在页面的底部,接近它ob_flush();之前?>标记,以便你喜欢的东西:
)
<?php
include('connexion.php');
function outout_errors($error) {
echo '<ul><li>',$error.'</li></ul>';
}
if($_POST) {
$titre = mysql_real_escape_string(strip_tags($_POST['titre']));
$annee = mysql_real_escape_string(strip_tags($_POST['annee']));
$Gmusical = mysql_real_escape_string(strip_tags($_POST['Gmusical']));
$picture_tmp = $_FILES['img']['tmp_name'];
$picture_name = $_FILES['img']['name'];
$picture_type = $_FILES['img']['type'];
$allowed_type = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg');
if(in_array($picture_type, $allowed_type)) {
$path = 'images/'.$picture_name; //change this to your liking
} else {
$error[] = 'File type not allowed';
}
if(!is_numeric($annee)) {
$error[] = $annee.' is not a number';
}
if(!empty($error)) {
echo '<font color="red">'.output_errors($error).'</font>';
} else if(empty($error)) {
$req="INSERT INTO `cd` (`titre`, `Annee`, `genremusical`, `Image`) VALUES ('$titre', '$annee', '$Gmusical', '$path')";
move_uploaded_file($picture_tmp, $path);
if (mysql_query($req)) {
echo 'ok';
} else {
echo 'ko';
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td><strong>Titre</strong></td>
<td><input name="titre" type="text"></td>
</tr> <tr>
<td><strong>Annee</strong></td>
<td><input name="annee" type="text"></td>
</tr><tr>
<td><strong>Genre musical</strong></td>
<td><input name="Gmusical" type="text"></td>
</tr><tr>
<td><strong>Picture</strong></td>
<td><input type="file" name="img"></td>
</tr>
<tr>
<input type="submit">
</tr>
</table>
</form>
回答by Stefan
You have to set the column-type of the "image" column in the databse to BLOB or LONGBLOB (or others), for example:
您必须将数据库中“图像”列的列类型设置为 BLOB 或 LONGBLOB(或其他),例如:
CREATE TABLE cd (
...
Image LONGBLOB,
);
And then simply insert the data like you did.
然后像您一样简单地插入数据。
A usually better way to solve this is to store the file in the filesystem and only save the path to the file in the database. If you want to do so, you may want to check out this SO question. (See also this one.)
通常更好的解决方法是将文件存储在文件系统中,并且只将文件的路径保存在数据库中。如果你想这样做,你可能想看看这个 SO question。(另见这个。)
(As user Fred -ii-pointed out in the comment, you will also have to set the enctypeof the form-Tag to "multipart/form-data".)
(正如用户Fred -ii-在评论中指出的,您还必须将表单-Tag的enctype设置为“multipart/form-data”。)
回答by shahsikant verma
<?php include("config.php");?>
<?php
error_reporting("0");
if(isset($_POST['submit'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
$image_name= $_POST['image_name'];
// Image file directory
$target = "images/".basename($image);
// Now insert query
$sql = "INSERT INTO addimage(image, image_name) values ('$image','$image_name')";
// Execute query
mysqli_query($dbcon,$sql);
if(move_uploaded_file($_FILES['image']['tmp_name'],$target))
{
$img = "image uploading successfully";
} else {
$img = "faild image uploading";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload image with text Field</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<div class="col-sm-10">
<h1>Image uploading with text field</h1>
<div class="card-box">
<div class="row">
<div class="col-md-6">
<div class="p-20">
<form method="post" enctype="multipart/form-data">
<h5 class="alert-success">
<?php if(isset($img)){ echo $msg;}?>
</h5>
<div class="form-group">
<label class="control-label">Image Name</label>
<input type="text" class="form-control" data-size="sm" name="image_name">
</div>
<div class="form-group">
<label class="control-label">Small file style</label>
<input type="file" class="filestyle" data-size="sm" name="image">
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="Save" name="submit">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

