2014-12-08 43 views
1

我的圖片上傳表單工作正常。保存數據庫的名稱和路徑。我想要保存完整的網址,而不是僅僅使用名稱來形象。我的意思是現在在DB中保存file_name.jpg我想要保存http://example.com/images/file_name.jpg。這裏是upload.php當圖片上傳時在數據庫中保存完整的URL鏈接

  define('MAX_FILE_SIZE', 20000000430); 
      $uploadDir = "../../img/"; 
      $permitted = array('image/jpeg', 'image/jpeg', 'image/png', 'image/gif');    

      $fileName = $_FILES['image']['name']; 
      $tmpName = $_FILES['image']['tmp_name']; 
      $fileSize = $_FILES['image']['size']; 
      $fileType = $_FILES['image']['type']; 

      // make a new image name 
      $ext = substr(strrchr($fileName, "."), 1); 
      // generate the random file name 
      $randName = md5(rand() * time()); 

      // image name with extension 
      $myFile = $randName . '.' . $ext; 
      // save image path 
      $path = $uploadDir . $myFile; 
      if (in_array($fileType, $permitted)) 
      { 
       $result = move_uploaded_file($tmpName, $path); 
        if (!$result) 
        { 
          echo "Error uploading image file"; 
          exit; 
        } 
        else 
        {       
         // keep track post values 
         $name = $_POST['name']; 
         $description = $_POST['description']; 
         // update data 
         $pdo = Database::connect(); 
         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
         $sql = "UPDATE food set name = ?, image = ?, path = ?, description = ? 
           WHERE id = ?"; 
          $q = $pdo->prepare($sql); 
          $q->execute(array($name,$myFile,$path,$description,$id)); 
          Database::disconnect(); 
          echo "<code>Information updated!!</code>"; 
        } 
       } 

我試着把URL放在$ uploadDir中。

$uploadDir = "http://example.com/img/"; 

但我得到這個錯誤。

Warning: move_uploaded_file(http://example.com/img/75a13564a8f3305fb0a30ab95487b8de.jpg): failed to open stream: HTTP wrapper does not support writeable connections 

也試過這樣的事情,得到了同樣的錯誤

define('domainURL',   'http://'.$_SERVER['HTTP_HOST']); 
$path = domainURL . $uploadDir . $myFile; 
+0

不move_uploaded_file把全名,當你在數據庫中插入鏈接,然後創建完整的url並保存在db – Zeeshan 2014-12-08 06:52:35

+0

如果我正確理解你的意思是什麼時候重寫'$ myFile'具有完整的URL時插入分貝? – 2014-12-08 07:04:48

回答

1

您可以使用相對或絕對路徑。 相對路徑是「../../img/」; 絕對路徑應該像「/ www/htdocs/img /」; (你可以在FTP客戶端看到絕對路徑) 而且你不能使用URL。 對於存儲在數據庫使用另一個變量,URL

+0

謝謝你的回答。所以你的意思是沒有辦法做我想要的東西? – 2014-12-08 07:03:24

+0

對於在數據庫中存儲使用另一個變量與URL – CLARK 2014-12-08 07:04:00

+0

而我將在sql查詢中使用此變量,而不是我當前?你能給個例子嗎? – 2014-12-08 07:08:10

2

move_uploaded_file函數不接受文件的URL。

它接受圖像的實際路徑。

因爲您的文件正在物理移動。

例如$path = $_SERVER['DOCUMENT_ROOT'] . 'img/'

+0

你不瞭解我或我沒有正確解釋。我在DB路徑中有兩個字段,分別是'path'和'path'我想在名稱中保存'../ img /'我想要完整的url http:// example.com/app/img /'。當我嘗試你的例子時,我得到了像'http:// example.com/home/venb/public_html/app/img/image.jpg'這樣的url。 – 2014-12-08 06:57:55

0

在一個PHP變量,將其存儲在數據庫中使用$路徑指定上傳路徑..

$path = "www.sitename.com/uploads/".$filename ; 
+0

這是不可能的警告:move_uploaded_file(www.example。 COM /應用/ IMG/598224af00414b2a66cbb2554fe8eefa。JPG):未能打開流' – 2014-12-08 08:14:04

+0

親愛的朋友不要使用$路徑來移動上傳的文件..我只是要求將完整的數據存儲在不同的變量中。 – 2014-12-08 13:03:23

相關問題