这是我从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!';
}
}
?>
答案 0 :(得分:2)
回答你的问题(如果没有$ _FILEs ['userfile'] ['name']&amp; $ _FILEs ['userfile'] ['tmp_name']),这个脚本如何工作: 没有它它确实有效,因为数组键在此处被指定为属性:
$this->{$key}=$value;
因此,现在可以将其称为
,而不是$ _FILES ['userfile'] ['tmp_name']。$this->tmp_name
您已经回复了为什么它可能无法在所有浏览器中使用。
希望有所帮助, 斯蒂芬