2014-09-11 45 views
1

我試圖將.jpg圖像插入到mysql表中,但它不工作。如何上傳圖像到mysql表

它顯示了以下錯誤消息:

警告:file_get_contents()函數:文件名不能爲空

如何解決呢?

這是我的代碼:

<form action="image.php" method="POST" enctype="multipart/form-data"> 
    <label>File: </label><input type="file" name="image" /> 
    <input type="submit" /> 
</form> 


<?php 
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); 
$image_name = addslashes($_FILES['image']['name']); 
$sql = "INSERT INTO `images` (image,image_name) VALUES ('$image', '$image_name')"; 
if (!mysql_query($sql)) { // Error handling 
    echo "Something went wrong! :("; 
} 

?> 
+0

這一警告是因爲每個頁面加載它的企圖即使沒有發佈任何內容,也可以將記錄插入數據庫中。將你的PHP代碼包裝在一個'if(!empty($ _ FILE ['image'])){...}'check中。 – Tom 2014-09-11 06:16:04

回答

1

首先,你應該檢查你的圖像列是BLOB類型!

我不知道任何關於你的SQL表,但如果我會嘗試以我自己爲例。

我們得到了字段id(int),image(blob)和image_name(varchar(64))。

因此,代碼應該是這樣的(假設ID始終是「1」,讓我們用這個的mysql_query):

$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence! 
$image_name = addslashes($_FILES['image']['name']); 
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')"; 
if (!mysql_query($sql)) { // Error handling 
    echo "Something went wrong! :("; 
} 

你做錯了在許多方面。不要使用mysql函數 - 它們已被棄用!使用PDO或MySQLi。您還應該考慮將文件位置存儲在磁盤上。使用MySQL存儲圖像被認爲是Bad Idea™。使用圖像等大數據處理SQL表可能會產生問題。

此外,您的HTML表單不符合標準。它應該是這樣的:

<form action="insert_product.php" method="POST" enctype="multipart/form-data"> 
    <label>File: </label><input type="file" name="image" /> 
    <input type="submit" /> 
</form> 
1

的file_get_contents方法不用於保存JPG圖片,它是用來讀取文件內容爲一個字符串。因爲它將使用內存映射技術,如果服務器支持這種技術,可以提高性能。所以,你應該首先創建一個文件夾到根文件夾或任何你想要的根文件夾內的任何地方,然後設置保存jpg圖像的路徑。

    <?php 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $temp = explode(".", $_FILES["image"]["name"]); 
    $extension = end($temp); 

    if ((($_FILES["image"]["type"] == "image/gif") 
    || ($_FILES["image"]["type"] == "image/jpeg") 
    || ($_FILES["image"]["type"] == "image/jpg") 
    || ($_FILES["image"]["type"] == "image/pjpeg") 
    || ($_FILES["image"]["type"] == "image/x-png") 
    || ($_FILES["image"]["type"] == "image/png")) 
    && ($_FILES["image"]["size"] < 20000) 
    && in_array($extension, $allowedExts)) { 
     if ($_FILES["image"]["error"] > 0) { 
     echo "Return Code: " . $_FILES["image"]["error"] . "<br>"; 
     } else { 
     echo "Upload: " . $_FILES["image"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["image"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["image"]["size"]/1024) . " kB<br>"; 
     echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>"; 
     if (file_exists("upload/" . $_FILES["image"]["name"])) { 
      echo $_FILES["image"]["name"] . " already exists. "; 
     } else { 
      $file_name = $_FILES["image"]["name"]; 
      move_uploaded_file($_FILES["image"]["tmp_name"], 
      "upload/" . $file_name); 
      echo "Stored in: " . "upload/" . $file_name; 
      $sql = "INSERT INTO images (image,image_name) VALUES ('$image', '$file_name')"; 
      if (!mysql_query($sql)) { // Error handling 
       echo "Something went wrong! :("; 
      } 
     } 
     } 
    } else { 
     echo "Invalid file"; 
    } 
    ?> 
+0

我嘗試插入jpg圖像。但它顯示無效的文件 – Tje 2014-09-11 06:32:07

+0

通過使用我的代碼? – WordpressCoder 2014-09-11 06:34:32

+0

請用$ _FILES [「image」]替換$ _FILES [「image」],因爲您的字段名稱是image.So請使用print_r($ _ FILES),在這裏您可以找到您的整個數據並使用它。 – WordpressCoder 2014-09-11 06:36:07

0

檢查下面的代碼:

--- HTML ---

<form enctype="multipart/form-data" action="add.php" method="POST"> 
Image: <input type="file" name="photo"> 
<br> 
<input type="submit" value="Add"> 
</form> 

- PHP -

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename($_FILES['image']['name']); 
//This gets all the other information from the form 
$image_name=mysql_real_escape_string(addslashes($_FILES['image']['name'])); 
$image = mysql_real_escape_string(addslashes(file_get_contents($_FILES['image']['tmp_name']))); 
// Connects to your Database 
mysql_connect("YOUR_HOST", "USERNAME", "PSSWORD") or die(mysql_error()) ; 
mysql_select_db("YOUR_DATABASE") or die(mysql_error()) ; 
//Writes the information to the database 
mysql_query("INSERT INTO `product_images` VALUES ('', '$image', '$image_name')") ; 
//Writes the photo to the server 
if(move_uploaded_file($_FILES['image']['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."; } 
?> 
+0

這是顯示警告:move_uploaded_file(images/1.jpg):無法打開流:沒有這樣的文件或目錄在C:\ wamp \ www \ test \ image.php在第12行 – Tje 2014-09-11 06:39:00

+0

請確保if你已經創建了文件夾images /! – 2014-09-11 09:44:33