2013-07-14 98 views
0

所以我做了一個圖像託管網站和即時通訊嘗試添加一個選項來下載圖像。我有一個腳本,它的工作原理是,所有的圖像都按日期存儲在文件夾中。所以在腳本上它只是一個靜態url。如何使這變成像一個功能,將其添加到實際的圖像頁面,如「www.mysite.com/view-image/123456.png?download」圖像下載功能

繼承人的代碼:

<?php 
if (!$filename) { 
     // if variable $filename is NULL or false display the message 
     echo $err; 
    } else { 
     // define the path to your download folder plus assign the file name 
     $path = 'image.uploads/' .$imagedate/'.$filename; 
     // check that file exists and is readable 
     if (file_exists($path) && is_readable($path)) { 
      // get the file size and send the http headers 
      $size = filesize($path); 
      header('Content-Type: application/octet-stream'); 
      header('Content-Length: '.$size); 
      header('Content-Disposition: attachment; filename='.$filename); 
      header('Content-Transfer-Encoding: binary'); 
      // open the file in binary read-only mode 
      // display the error messages if the file can´t be opened 
      $file = @ fopen($path, 'rb'); 
      if ($file) { 
       // stream the file and exit the script when complete 
       fpassthru($file); 
       exit; 
      } else { 
       echo $err; 
      } 
     } else { 
      echo $err; 
     } 
    } 
?> 
+0

所以你在$路徑上工作?爲什麼不在這裏查詢並獲得img匹配呢?我不知道一些ID? –

+0

你是什麼意思?即時通訊新的PHP。你能幫我重寫一下嗎?我需要它能夠在圖像下方編碼。對於IDS,只是讓他們和生病改變他們 – VladL

+0

你說這是工作?即時通訊與你說的話混淆,這是行不通的?你正在建造它 –

回答

0

如果我收到了你的問題的權利,你要當用戶導航到該路徑的文件被下載..

您可以使用.htaccess強制下載文件,而不是在瀏覽器中查看它..

+0

是啊,我想做一個下載按鈕,用戶點擊按鈕下載圖像 – VladL

+0

即時看到他想要這個腳本,所以他想能夠從視圖圖像文件調用這個腳本,所以他想從那個頁面下載download_file.php的功能,麻煩的是試圖用正確的圖像文件名稱來調用這個鏈接,這個即時通訊猜測將由一些數據庫查詢 –

+0

你可以製作一個下載按鈕,將目標指向特定的文件夾並添加一個.htaccess文件,以強制下載文件。假設路徑是xyz/abc.png,您可以在xyz文件夾中添加.htaccess,這將強制下載abc.png圖像。 –