【问题标题】:Php OOps file upload working scriptphp OOps 文件上传工作脚本
【发布时间】:2012-04-26 13:34:31
【问题描述】:

这是我从 devshed 获得的脚本。它在 Opera 和其他(而不是 IE)中运行良好。我的问题是:没有$_FILES['userfile']['name'] & $_FILES['userfile']['tmp_name'],这个脚本如何工作?

<?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!';
    }
    }
?> 

【问题讨论】:

  • 你在enctype= multipart/form-data表格中提到了enctype
  • 是的,没关系......这个脚本有效......只是想知道 --- 没有 $_FILEs['userfile']['name'] & $_FILEs['userfile']['tmp_name' ] 这部分..我是新手,所以
  • @jathin check out stackoverflow.com/questions/10291655/php-move-uploaded-file/… 更干净,更安全,不抛出异常,返回漂亮的错误消息

标签: php file upload


【解决方案1】:

回答您的问题(在没有 $_FILEs['userfile']['name'] & $_FILEs['userfile']['tmp_name'] 的情况下此脚本如何工作): 没有它它确实可以工作,因为数组键在这里被分配为属性:

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

因此,现在可以将其称为

,而不是 $_FILES['userfile']['tmp_name']
$this->tmp_name

您已经收到了关于为什么它可能不适用于所有浏览器的回复。

希望对您有所帮助, 斯蒂芬

【讨论】:

  • 谢谢队友...是的...我对 stackoverflow 上的快速响应感到惊讶。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多