我目前正在爲上傳圖片寫上傳類。我做了擴展檢查,以確認上傳的圖像是支持的類型,並且當上傳的文件被複制到其休息的地方時,照片始終是chmod(0664)。這是相對安全的嗎?我對圖像編碼知之甚少,但即使有人經歷了以某種方式欺騙我的擴展檢查的麻煩,該文件也永遠無法在服務器上運行,除非其他地方存在安全漏洞,並且攻擊者已經加入我的文件系統,對嗎?這裏是我的擴展檢查:文件上傳和安全
function validate_ext() { //Function validates that the files extension matches the list of allowed extensions
$extension = $this->get_ext($this->theFile);
$ext_array = $this->extensions;
if (in_array($extension, $ext_array)) { //Check if file's ext is in the list of allowed exts
return true;
echo "ext found";
} else {
$this->error[] = "That file type is not supported. The supported file types are: ".$this->extString;
return false;
}
}
而這裏的功能是將上傳的文件複製到它的最後安息的地方。
if ($_FILES[$this->uploadName]['error'] === UPLOAD_ERR_OK){
$newfile = $this->uploadDir.$this->theFile;
if (!move_uploaded_file($this->tempFile, $newfile)) {
$this->error[] = "The file could not be moved to the new directory. Check permissions and folder paths.";
die($this->error_text());
}else{
$this->error[] = "The file ".$this->originalName." was successfully uploaded.";
if ($this->renameFile == true){
$this->error[] = $this->originalName." was renamed to ".$this->theFile;
}
chmod($newfile , $this->fileperm);
}
}else{
$this->error[] = $this->file_upload_error_message($_FILES[$this->uploadName]['error']);
die($this->error_text());
}
要真正確定什麼都不會以某種方式執行,只需在它周圍構建一個包裝腳本。包裝腳本應該做一個頭(「Content-type:$ mime_type」)並將文件內容轉儲到標準輸出。要確定MIME類型,請查看fileinfo pecl擴展名(http://us3.php.net/manual/en/ref.fileinfo.php)。 – Friek