2014-11-01 55 views
1

我想上傳一個圖像使用PHP到MySQL。在查詢中,我試圖將圖像和圖像的目錄保存在數據庫中,但是我在數據庫中獲得了空目錄,並且在文件夾中沒有圖像。任何幫助將不勝感激!上傳一個圖像使用PHP到MySQL

<?php 
// include db connect class 
define('__ROOT__', dirname(dirname(__FILE__))); 
require_once(__ROOT__.'/android_connect/db_connect.php'); 

$db = new DB_CONNECT(); 


//Setting up images directory 
$target = "./images"; 
$target = $target . basename($_FILES['photo']['name']); 

$photo=($_FILES['photo']['name']); 

//inserting data order 
$order = "INSERT INTO scb 
     (photo, directory) 
     VALUES 
     ( '$_POST[$target]', 
     '({$_FILES['photo']['name']})')"; 

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename($_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives an error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 

//declare in the order variable 
$result = mysql_query($order); //order executes 
if($result){ 
echo "Thank you for submitting!"; 
} else{ 
echo "Sorry, something went wrong! Please try again!"; 
} 
?> 
+1

嘿,請確保你有,你要上傳的文件夾適當的訪問權限。您也可以通過打印出以下值來獲取錯誤: $ _FILES [「photo photo」] [「error」] – Azarus 2014-11-01 21:27:54

+0

檢查錯誤並檢查表單(請提供表單html)。 – sinisake 2014-11-01 21:31:54

+2

我正在使用Android應用程序上傳圖像,我用另一個代碼上傳圖像到文件夾,它的工作原理,但是當我想將圖像保存到Mysql時,我得到這個錯誤「對不起,有一個問題上傳你的文件。「這意味着圖像沒有上傳!我會嘗試打印出$ _FILES [「photo」] [「error」]的值,然後回覆給你們 – sasuri 2014-11-01 21:35:58

回答

1

那麼首先mysql_query是過時的使用PDO代替(你必須檢查,如果該功能是通過使用phpinfo()您的服務器上啓用);

試試這個

<?php 
      //Connect to sql db 
      try { 
        $user = "username"; 
        $pass = "password"; 
        $db = new PDO('mysql:host=yourhost;dbname=yourdb', $user, $pass); 

      } catch (PDOException $e) { 
        print "Error!: " . $e->getMessage() . "<br/>"; 
        die(); 
      } 



      //Setting up images directory 
      $target = "./images/"; //you forgot the last slash 
      $target = $target . basename($_FILES['photo']['name']); 

      $photo = $_FILES['photo']['name']; 

      //inserting data order 
      $stmt = $db->prepare("INSERT INTO scb (photo, directory) VALUES (:photo, :directory)"); 
      $stmt->bindParam(':photo', $_POST[$target]); 
      $stmt->bindParam(':directory', $_FILES['photo']['name']); 


      if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
      { 

        //Tells you if its all ok 
        echo "The file ". basename($_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
      } 
      else { 

        //Gives an error if its not 
        echo "Sorry, there was a problem uploading your file."; 
      } 

      //declare in the order variable 
      if($stmt->execute()){ 
        echo "Thank you for submitting!"; 
      } else{ 
        echo "Sorry, something went wrong! Please try again!"; 
      } 
    ?> 
+2

得到此錯誤錯誤!:SQLSTATE [42000] [1044]訪問拒絕用戶'a5983957****'@'10.1.1.22'到數據庫'a5983957 ***'任何想法如何處理? – sasuri 2014-11-01 23:14:26

+1

檢查您的主機,數據庫名稱,用戶名和密碼 – Ahmad 2014-11-02 12:44:28

0

您是否檢查過Web服務器日誌文件中的錯誤或警告?你得到任何合理的輸出,如果你寫:

print_r($_FILES);