Php 上传文件、添加到数据库并向用户显示
我一直在尝试上载一个文件,将其保存到数据库,然后向用户显示其上载的文件。 这是图像文件的数据库Php 上传文件、添加到数据库并向用户显示,php,mysql,Php,Mysql,我一直在尝试上载一个文件,将其保存到数据库,然后向用户显示其上载的文件。 这是图像文件的数据库 Field Type NULL DeFault ID int NO PIC varchar(255) NO 下面是上传文件的HTML <form method="post" action="addMember.php" enctype="multipart/form-data"> <p&
Field Type NULL DeFault
ID int NO
PIC varchar(255) NO
下面是上传文件的HTML
<form method="post" action="addMember.php" enctype="multipart/form-data">
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<input TYPE="submit" name="upload" value="Add Picture"/>
</form>
照片:
下面是后面的php文件:-
<?php
include 'dbconnector.php';
//This is the directory where images will be saved
$target = "/img";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$pic=($_FILES['photo']['name']);
// Connects to your Database
//Writes the information to the database
$query="INSERT into testImage VALUES(NULL, '" . $pic . "')";
$result=mysql_query($query,$db) or die (mysql_error($db));
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
我很确定目录和文件名之间缺少/。应该是:
$target = $target .'/'. basename( $_FILES['photo']['name']);
$target
中缺少目录分隔符。您可以在回显变量时看到问题
$target = $target . "/" . basename( $_FILES['photo']['name']);
使用此选项显示图片:
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
// print the image
echo '<img src="' . $target . '" alt="uploaded image" />';
}
//将照片写入服务器
如果(移动上传的文件($文件['photo']['tmp\u name'],$target))
{
//告诉你一切是否正常
echo“The file”.basename($_FILES['uploadedfile']['name'])”已上载,并且您的信息已添加到目录中;
//打印图像
回声';
}
编辑:
从数据库获取图像:
// connect to database
$query = mysql_query('SELECT pic FROM testImage WHERE id = 1;',$db) or die (mysql_error($db));
$image = mysql_result($result, 0);
echo "<img src=\"/img/$image\" alt=\"uploaded image\" />";
//连接到数据库
$query=mysql\u query('selectpicfromtestimage,其中id=1;',$db)或die(mysql\u error($db));
$image=mysql\u结果($result,0);
回声“;
您做了哪些故障排除工作?目录可写吗?@johncode实际上没有太多故障排除。我更改了目录,更改了路径,但没有任何效果。Nouphal.M-是的,目录是可写的。move_uploaded_文件($_FILES['photo']['tmp_name']],$target)中$target的值是多少?我很确定在目录和文件名之间缺少/。它应该是$target=$target.'/'。basename($_文件['photo']['name']);谢谢,这很有效。但是显示部分不起作用,另外,我需要从数据库中获取文件。我该怎么做?你可以像我的新例子一样把它们拿来。谢谢,这很有效。如何显示数据库本身的图片?