2013-07-13 71 views
-2
獲取最新的PIC

我使用下面的代碼將圖像存儲在我的web服務器:無法從PHP

function SavePic() 
{ 
    $allowedExts = array("jpeg", "jpg"); 
    $temp = explode(".", $_FILES["UserPic"]["name"]); 
    $extension = end($temp); 

    if ((($_FILES["UserPic"]["type"] == "image/jpeg") 
    || ($_FILES["UserPic"]["type"] == "image/jpg")) 
    //&& ($_FILES["UserPic"]["size"] < 2097152) 
    && in_array($extension, $allowedExts)) 
    { 
     if ($_FILES["UserPic"]["error"] > 0) 
     { 
      echo json_encode("Error: ".$_FILES["UserPic"]["error"]); 
     } 
     else 
     {  
      $folder = "/home5/username/public_html/Project/Users/Images/";     
      echo move_uploaded_file($_FILES["UserPic"]["tmp_name"],$folder.$_REQUEST["email"].".".$extension); 
     } 
    } 
    else 
    { 
     echo json_encode("Invalid file"); 
    } 
} 

而下面的代碼來獲取圖像:如果我的電子郵件是

function RetrievePic() 
{ 
    $handle = fopen('php://input','r'); 
    $jsonInput = fgets($handle); 
    // Decoding JSON into an Array 
    $retrieveParameters = json_decode($jsonInput,true);   

    $UserPic = array("UserPic" => "http://www.mysite.com/Project/Users/Images/".$retrieveParameters['email']."."."jpg"); 
    echo json_encode($UserPic); 
} 

對於實例ABC @ xyz.com然後圖像將被存儲爲「[email protected]」。問題是,當我嘗試覆蓋圖像以替換舊圖像時,服務器每次都會返回舊圖像。

更新: 當我把網址在瀏覽器e.g http://www.mysite.com/Project/Users/Images/[email protected] 最新的圖像顯示之後,我開始接受最新的圖像。

+3

您可能會在Web服務器是如何緩存數據想看看,你總是可以添加一個時間戳到URL(也許文件修改時間?)'http://www.mysite.com/Project/Users/Images/[email protected]?modifiedtime' – hank

+0

就像@hank剛纔寫的:試試'$ UserPic = array(「UserPic」=>「http://www.mysite.com/Project/Users/Images/".$retrieveParameters['email']。」。 「。」jpg?「。time());' –

+1

@MarcinKrawiec使用'time()'每次都會強制重載,這是浪費帶寬,最好使用文件的實際修改時間。 – hank

回答

0

這看起來像是一個緩存問題。您是否確認新圖片在服務器上正確保存?

如果圖片保存正確,那麼您應該在RetrievePic例程中添加一些標題以防止它被緩存。另請參閱:Disable cache for some images

+0

是我的服務器上保存了新照片。 –

0
  • 我不建議您使用擴展名處理文件。 它可以很容易地僞裝成
    另外$_FILES['UserPic']['type']不可靠。
  • 根據PHP版本5.4.1,有關於$ _FILES的嚴重安全漏洞。
    • 目錄遍歷攻擊
    • $ _FILES收起攻擊

你應該這樣做:

<?php 

// Configure 
$upload_key  = 'UserPic'; 
$max_filesize = 2097152; // Bytes 
$save_directory = '/home5/username/public_html/Project/Users/Images'; 

if (version_compare(PHP_VERSION, '5.4.1') < 0) { 
    die('This PHP Version has serious security hole concerning $_FILES.'); 
} 

if (isset($_FILES[$upload_key])) { 

    try { 

     $error = $_FILES[$upload_key]['error']; 

     if (is_array($error)) { 
      throw new Exception('This script can\'t accept multiple files'); 
     } 

     switch ($error) { 
      case UPLOAD_ERR_INI_SIZE: 
       throw new Exception('Exceeded upload_max_filesize'); 
      case UPLOAD_ERR_FORM_SIZE: 
       throw new Exception('Exceeded MAX_FILE_SIZE'); 
      case UPLOAD_ERR_PARTIAL: 
       throw new Exception('Incomplete file uploaded'); 
      case UPLOAD_ERR_NO_FILE: 
       throw new Exception('No file uploaded'); 
      case UPLOAD_ERR_NO_TMP_DIR: 
       throw new Exception('No tmp directory'); 
      case UPLOAD_ERR_CANT_WRITE: 
       throw new Exception('Couldn\'t write data'); 
      case UPLOAD_ERR_EXTENSION: 
       throw new Exception('Extension error'); 
     } 

     $name  = $_FILES[$upload_key]['name']; 
     $tmp_name = $_FILES[$upload_key]['tmp_name']; 
     $size  = $_FILES[$upload_key]['size']; 

     if ($name === '') { 
      throw new Exception('Invalid filename'); 
     } 

     if ($size > $max_filesize) { 
      throw new Exception(sprintf('Exceeded %d bytes limit', $max_filesize)); 
     } 

     if (!is_uploaded_file($tmp_name)) { 
      throw new Exception('Not an uploaded file'); 
     } 

     $finfo = new finfo(FILEINFO_MIME); 
     $type = $finfo->file($tmp_name); 

     if ($type === false) { 
      throw new Exception('Failed to get MimeType'); 
     } 

     if (substr($type, 'image/jpeg') !== 0) { 
      throw new Exception('Only JPEG images available'); 
     } 

     if (!isset($_REQUEST['email']) || !is_string($email = $_REQUEST['email']) || $email === '') { 
      throw new Exception('E-mail address required'); 
     } 

     if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { 
      throw new Exception('Invalid E-mail address'); 
     } 

     $new_name = $save_directory.'/'.$email.'.jpg'; 

     if (is_file($new_name)) { 
      throw new Exception('The file already exists'); 
     } 

     if ([email protected]_uploaded_file($tmp_name, $new_name)) { 
      throw new Exception('Failed to move uploaded file'); 
     } 

     $msg = "File successfully uploaded as {$new_name}"; 

    } catch (Exception $e) { 

     $msg = 'Error: '.$e->getMessage(); 

    } 

} else { 

    $msg = 'No file sent'; 

} 

echo json_encode($msg);