簡體   English   中英

使用PHP將Image上傳到MYSQL DB

[英]Uploading Image to MYSQL DB using PHP

還有一個我堅持的PHP問題,所以這就是我想要做的和我的代碼:

第一步 - 嘗試將名稱,圖像和ID上載到SQL表。 該表名為second,有4個字段。 我在另一個名為admin的表中使用其中一個字段作為FK,而FK是id。

什么工作? * 當我單擊提交按鈕時,圖像的鏈接會上傳,但實際上我的文件夾中沒有圖像。 當我嘗試上傳表單時,我想使用用戶的ID並將其放入新表中,以便我可以引用該字段來顯示數據。 我覺得這很難解釋 *

這里編輯 - 實現Id字段,favname字段,不記錄任何信息,並且鏈接沒有正確上傳到文件夾。

任何幫助將不勝感激:在這里。

SecondPic.php

<?php
include "common.php";
$secondid = $_GET['id'];
DBConnect();



$Link = mysql_connect($Host, $User, $Password);

//This is the directory where images will be saved 
 $target = "second/"; 
 $target = $target . basename( $_FILES['photo1']['name']); 


$favname = $_POST["name1"];
$pic2=($_FILES['photo1']['name']); 
$id = $_POST["$formID"];



$Query ="INSERT into $Table_2 values ('0', '$id', '$favname', '$pic2')";

if (mysql_db_query ($DBName, $Query, $Link)){
print ("A record was created <br><a href=index.php> return to index </a>\n");

 // Connects to your Database 
 //mysql_connect("localhost", "jonathon_admin", "hello123") or die(mysql_error()) ; 
 //mysql_select_db("jonathon_admin1") or die(mysql_error()) ; 


 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo1']['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."; 
 } 


} else {

print (" - Your Record was not created");   
}

mysql_close($Link);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

SecondUpload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="secondPic.php">
  <p>
    <label for="name1">Fav Location Name: </label>
  <input type="text" name="fav1" id="fav1" />
  </p>
  <p>
  <label for="photo1">Fav Location Photo: </label>
 <input type="file" name="photo1"><br> 
  </p>
  <p>
  <label for="formID">ID: <? echo $rows['id']; ?> </label>
  <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>



</body>
</html>

之前使用過這個,我最大的建議是不要在數據庫中存儲圖像(或任何二進制數據)。 它導致許多問題,將來會引起問題。 相反,我建議在本地存儲文件並保持對象的相對路徑。

檢查這些線程關於MYSQL存儲圖像為BLOB。

要做或不做:將圖像存儲在數據庫中

使用php在MySql數據庫中插入Blob

一般建議是:不要將圖像存儲在MySQL中,因為增長難以管理,性能可能會降低。 此外,動態使用您的圖像需要您將其轉換回來,但需要支付額外費用。 沒有比你自己的服務器操作系統更好,更便宜的文件處理程序。

Aftr看看你的帖子是完整的,標題誤導我(可能是上面的那個人)認為你想將圖像數據直接存儲到數據庫中。

那不是你想要做的。

在我們繼續之前,以下是我的問題。

$target = "second/"

這個腳本是“SecondPic.php”在基礎文件夾中嗎? 和同一個基本文件夾中的文件夾“second”?

通常最好在move_uploaded_file()中給出一個絕對路徑,所以$ target應該是這樣的:

$target = dirname(__FILE__) . DIRECTORY_SEPARATOR . $_FILES['photo1']['name'];

其次,最好在保存到數據庫之前移動文件,因為移動文件的錯誤概率更高。

第三,有任何錯誤嗎? 在日志或頁面輸出?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM