2014-12-06 75 views
1

即時通訊使用xampp本地主機開發網站。我想添加圖像使用phpmyadmin,但我不能添加它..圖像可以顯示在phpmyadmin,但不能出現在我的網站..是否有任何問題與我在php編碼?如何使用phpmyadmin在數據庫中插入圖像?

$host="localhost"; // Host name 
 
$username="root"; // Mysql username 
 
$password=""; // Mysql password 
 
$db_name="MMDB"; // Database name 
 
$tbl_name="artist"; // Table name 
 

 
// Create connection 
 
$conn = mysqli_connect($host, $username, $password, $db_name); 
 
// Check connection 
 
if (!$conn) { 
 
    die("Connection failed: " . mysqli_connect_error()); 
 
} 
 
$Name=$_POST['Name']; 
 
$Hometown=$_POST['Hometown']; 
 
$Income=$_POST['Income']; 
 
$Image=$_POST['Image']; 
 
//file =$_FILES['image']['tmp_name']; 
 
$Video=$_POST['Video']; 
 
$sql = "INSERT INTO $tbl_name(Name, Hometown, Income, Image, Video) 
 
VALUES ('$Name', '$Hometown','$Income','$Image','$Video')"; 
 

 

 
if (mysqli_query($conn, $sql)) { 
 

 
    include 'admin.php'; 
 
} else { 
 
    echo "x"; 
 
} 
 

 

 
mysqli_close($conn); 
 
?>


什麼在文件夾中添加圖像的代碼?

//其中我應該插入這個代碼 「> $圖像= $ _FILES [」 圖像 「] [」 名稱「]; $ UploadedFile的= $ _FILES [ '圖像'] [ 'tmp_name的值']; 如果($圖像){ $文件名=的stripslashes($ _ FILES [ '形象'] [ '名']);

  /* Ensure the file is JPG/PNG */ 
     if (($_FILES['image']['type'] != "image/jpg") && ($_FILES['image']['type'] != "image/jpeg") && ($_FILES['image']['type'] != "image/png")) { $error_status = 2; $error_message = "File uploaded was not a JPG or PNG file."; } 
     else { 
      /* Ensure the file is less than 10MB */ 
      $size = filesize($_FILES['image']['tmp_name']); 
      if ($size > (MAX_SIZE * 1024)) { $error_status = 3; $error_message = "File uploaded needs to be less than 10MB."; } 
      else { 
      $extension = "jpg"; 
      if(($_FILES['image']['type'] == "image/jpg") || ($_FILES['image']['type'] == "image/jpeg")) { $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } 
      else if (($_FILES['image']['type'] == "image/png")) { $extension = "png"; $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } 
      $img_mime_type = $_FILES['image']['type']; 

回答

0

在表中你不應該保存圖像

你應該創建一個文件夾來保存上傳的圖片。在你的上傳過程中,你可以複製文件夾中的圖片(使用散列獲取唯一的文件名),然後將文件名存儲在表中。

當您想使用圖片時,只需在filepath中打開圖片即可。

0

你可以保存圖像的路徑在數據庫中。

您可以將圖像複製到文件夾

同時觀看圖像。 你可以調用圖像的路徑

0

請不要將圖像保存在數據庫中。我知道你可以使用blob,但它會給你的數據庫帶來很大的壓力。就像我上面的2個答案一樣,最好將圖像保存到文件夾中,將路徑保存爲TEXT到數據庫中。然後調用使用

1

正如其他人形象地說,你應該避免在MySQL存儲圖像,但如果你仍然想這樣做,那麼它很簡單:

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="MMDB"; // Database name 
$tbl_name="artist"; // Table name 

// Create connection 
$conn = mysqli_connect($host, $username, $password, $db_name); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
//BTW: always escape your data 
$Name=mysqli_real_escape_string($conn,$_POST['Name']); 
$Hometown=mysqli_real_escape_string($conn,$_POST['Hometown']); 
$Income=mysqli_real_escape_string($conn,$_POST['Income']); 
$Image=mysqli_real_escape_string($conn,file_get_contents($_FILES['image']['tmp_name'])); 
$Video=mysqli_real_escape_string($conn,$_POST['Video']); 
$sql = "INSERT INTO $tbl_name (Name, Hometown, Income, Image, Video) 
VALUES ('$Name', '$Hometown','$Income','$Image','$Video')"; 


if (mysqli_query($conn, $sql)) { 

    include 'admin.php'; 
} else { 
    echo "x"; 
} 


mysqli_close($conn); 
?> 

根據數據的大小,您的腳本可能需要一段時間才能執行,更不用說其他人發佈的警告了。

要保存到一個文件夾

我相信你發佈的代碼可能是將圖像轉換爲另一種格式。這允許您存儲到一個文件夾:

$savePath = "/your/path" 
if($_FILES['image']['error'] != UPLOAD_ERR_OK) { 
     $error_status = 1; 
     $error_message = "There was an error uploading the file"; 
} 
/* Ensure the file is JPG/PNG */ 
elseif (($_FILES['image']['type'] != "image/jpg") && ($_FILES['image']['type'] != "image/jpeg") && ($_FILES['image']['type'] != "image/png")) { 
    $error_status = 2; 
    $error_message = "File uploaded was not a JPG or PNG file."; 
} 
else { 
    /* Ensure the file is less than 10MB */ 
    $size = filesize($_FILES['image']['tmp_name']); 
    if ($size > (MAX_SIZE * 1024)) { 
     $error_status = 3; 
     $error_message = "File uploaded needs to be less than 10MB."; 
    } 
    else { 
     //Save the image to your specified directory 
     $error_status = 0; 
     $error_message = ''; 
     move_uploaded_file($_FILES['image']['tmp_name'], $savePath.DIRECTORY_SEPARATOR.$_FILES['image']['name']); 
    } 
} 
//now the file should be uploaded, if error status is 0, then you can safely do this: 
$Image = $savePath.DIRECTORY_SEPARATOR.$_FILES['image']['name']; 
//and put that into the database "as text" 
+0

+1爲問題提供解決方案,而不僅僅是對OP的懲罰。雖然我同意圖像不應該存儲在數據庫中,但如果我希望/必須這樣做並且來找出如何,我想要一個答案 – 2017-05-01 03:55:04

相關問題