我想从用户那里获取一张图片并插入数据库。用户应该在下面的输入字段中上传文件。另外,我想在需要时显示数据库中的图像。
<label>Photo</label>
<input type="file" name="photo" required>
<?php
mysql_connect("localhost", "root", ""); mysql_select_db("prs");
if(isset($_POST['submit'])){
$photo = $_FILES['photo']['name'];
{
$query = mysql_query("insert into date (photo) values ('$photo')");
}
}
?>
答案 0 :(得分:0)
最好的方法是在数据库表中保存文件路径或文件名。并将您的文件保存在服务器中。
以下是根据您的需要上传和检索脚本的MULTIPLE示例示例。我将在这里使用3个文件。 front.html,upload.php,最后是preview.php
此文件包含基本的html表单内容。
<form enctype="multipart/form-data" action="upload.php" method="post">
<input id="uploadFile" type="file" name="files[]" class="img" />
<button class="btn btn-primary" name="up_img">POST</button>
</form>
2.upload.php
if(isset($_POST['up_img'])){ $errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name =$key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$pType='I';
$query="insert into img(user,FILE_NAME)values('$sUser','$file_name') ";
$desired_dir="images_folder";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
header('Location:preview.php');
}
}
3.preview.php
现在我已成功将我的图像/图像上传到服务器并准备检索它们。记住我的图像文件现在存储在文件夹调用images_folder中,它们的路径保存在img表中。
<?php
$sql=mysql_query('SELECT * FROM img ');
while($row = mysql_fetch_array($sql))
{
$sPic=$row['FILE_NAME'];
echo '<img src="images_folder/'.$sPic.'">';
}
?>
就是这样。这就是您将图像上传到服务器并检索它们的方式。
答案 1 :(得分:0)
所以这里有一个小选项来上传图片并将图片保存到mysql中 这不是刚刚打字的测试。
if(isset($_FILES['picture']) //checks for form post if not show form
{
$title = $_POST['title']; //get title
$desc = $_POST['description']; //get description
$target = "downloads/pics/".basename( $_FILES['uploaded']['name']); //this gets the target folder to save pictures make sure folder exists
$ok=1; //basic error set to 1
//This is our size condition
if ($uploaded_size > 20000000)
{
$out = "Your file is too large.<br>"; //checks for file size this can be changed to w/e
$ok=0; //if over error is set to 0
}
if ($ok==0)
{
$out.= "Sorry your file was not uploaded";
return $out;
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['picture']['tmp_name'], $target)) //tires to move the uploaded picture to target
{
$out = "The Picture: <b><u>".basename( $_FILES['picture']['name']). "</b></u> has been uploaded"; //if successfull then post was uploaded
$sql = "INSERT INTO database SET title=\"".$title."\",picture=\"".basename( $_FILES['uploaded']['name'])."\",description=\"".$desc."\""; //sql string to insert the folder and picture name
if (!mysql_query($sql))
{
die('Error In Picture Upload: - SQL INSERT INTO - '.date('m-d-y').' - SQL ERROR: '.mysql_error()); //if mysql error post error
}
else
{
$out.="<br /><br />Return To Your <a href=\"return location\">Downloads</a>"; //if mysql is ok show the return link
return $out;
}
}
}
else // if form is not submited then show form
{
$out ="\t\t<h3>".ucwords('new picture')."</h3>\r\n";
$out.="\t\t<form name=\"post\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" enctype=\"multipart/form-data\">\r\n";
$out.="\t\t\t<p>Title:<br /><input type=\"text\" class=\"inputtext\" name=\"title\" value=\"\" /></p>\r\n";
$out.="\t\t\t<p><b>".ucwords('picture upload')."</b>: <input type=\"file\" name=\"picture\" /></p>\r\n";
$out.="\t\t\t<p>Description:<br /><textarea id=\"text9\" name=\"description\"></textarea></p>\r\n";
$out.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords('upload')."\" /></p>\r\n";
$out.="\t\t</form>\r\n";
echo $out;
}