2012-04-26 63 views
-2

這是我從devshed得到的腳本。它在Opera和其他程序(不在IE中)中效果很好。我的問題是:這個腳本如何在沒有$_FILES['userfile']['name'] & $_FILES['userfile']['tmp_name']的情況下工作?Php OOps文件上傳工作腳本

<?php 
    class FileUploader 
    { 
    private $uploadFile; 
    private $name; 
    private $tmp_name; 
    private $type; 
    private $size; 
    private $error; 
    private $allowedTypes=array 
    ('image/jpeg','image/gif','image/png','text/plain','application/ms-word'); 

    public function __construct($uploadDir="./uploadfl/") 
    { 
    if(!is_dir($uploadDir)){ 
    throw new Exception('Invalid upload directory.'); 
    } 

    if(!count($_FILES)) 
    { 
    throw new Exception('Invalid number of file upload parameters.'); 
    } 

    foreach($_FILES['userfile'] as $key=>$value) 
    { 
    $this->{$key}=$value; 
    } 

    if(!in_array($this->type,$this->allowedTypes)) 
    { 
    throw new Exception('Invalid MIME type of target file.'); 
    } 

    $this->uploadFile=$uploadDir.basename($this->name); 

    } 
    // upload target file to specified location 

    public function upload(){ 
    if(move_uploaded_file($this->tmp_name,$this->uploadFile)){ 
    return true; 
    } 


    } 
    } 
    ?> 

    <?php 
     if($_POST['send']){ 
    //require_once 'fileuploader.php'; 
    $fileUploader=new FileUploader(); 
    if($fileUploader->upload()){ 
    echo 'Target file uploaded successfully!'; 
    } 
    } 
?> 
+1

你提到'enctype'形式'是enctype =多部分/格式data' – Rafee 2012-04-26 13:36:09

+0

耶所有沒關係......這個腳本工作..只是在想---沒有$ _FILEs ['userfile'] ['name']&$ _FILEs ['userfile'] ['tmp_name'] this part..iam new to oops so – jathin 2012-04-26 13:48:48

+0

@jathin check out http://stackoverflow.com/questions/10291655/ php-move-uploaded-file/10291918#10291918更清潔n安全和不會拋出異常,返回很好的錯誤信息 – 2012-04-26 13:54:05

回答

2

要回答你的問題(這個腳本是如何工作的,而不$ _FILES [ 'userfile的'] [ '名'] & $ _FILES [ 'userfile的'] [ 'tmp_name的值']): 它的工作離不開它因爲數組鍵被分配爲屬性的位置:

$this->{$key}=$value; 

所以不是$ _FILES [ 'userfile的'] [ 'tmp_name的值']現在可以被稱爲

$this->tmp_name 

你已經得到 關於爲什麼它可能不適用於所有瀏覽器的答覆。

希望幫助, 斯特凡

+0

感謝隊友...是的...我驚訝於這裏的快速反應在stackoverflow.Thanks每個機構。 – jathin 2012-04-26 13:52:44