我怎样才能变出一个班?
我想变出一个类,但似乎我没有做到正确。我怎样才能变出一个班?
下面是上传文件,我想只得到文件名后upload_inc.php
class upload
{
var $directory_name;
var $max_filesize;
var $error;
var $user_tmp_name;
var $user_file_name;
var $user_file_size;
var $user_file_type;
var $user_full_name;
function set_directory($dir_name =".")
{
$this->directory_name = $dir_name;
}
function set_max_size($max_file = 2000000)
{
$this->max_filesize = $max_file;
}
function error()
{
return $this->error;
}
function is_ok()
{
if(isset($this->error))
return FALSE;
else
return TRUE;
}
function set_tmp_name($temp_name)
{
$this->user_tmp_name = $temp_name;
}
function set_file_size($file_size)
{
$this->user_file_size = $file_size;
}
function set_file_type($file_type)
{
$this->user_file_type = $file_type;
}
function set_file_name($file)
{
$this->user_file_name = $file;
$this->user_full_name = $this->directory_name."/".$this->user_file_name;
echo $this->user_full_name;
}
function start_copy()
{
if(!isset($this->user_file_name))
$this->error = "You must define filename!";
if ($this->user_file_size <= 0)
$this->error = "File size error (0): $this->user_file_size KB<br>";
if ($this->user_file_size > $this->max_filesize)
$this->error = "File size error (1): $this->user_file_size KB<br>";
if($this->user_file_type != "image/jpeg")
$this->error = "the image must be jpeg extension";
if (!isset($this->error))
{
$filename = basename($this->user_file_name);
if (!empty($this->directory_name))
$destination = $this->user_full_name;
else
$destination = $filename;
if(!is_uploaded_file($this->user_tmp_name))
$this->error = "File " . $this->user_tmp_name . " is not uploaded correctly.";
if (!move_uploaded_file ($this->user_tmp_name,$destination))
$this->error = "Impossible to copy " . $this->user_file_name. " from " . $userfile . "to destination directory.";
echo 'test file' . $userfile;
}
}
}
在第二页中的代码。然后,我可以将文件名存储在我的数据库中。这是我的代码。
upload.php
// Defining Class
$uploaded = new upload;
// Set Max Size
$uploaded->set_max_size(350000);
// Set Directory
$uploaded->set_directory("data");
// Do not change
// Set Temp Name for upload, $_FILES['file']['tmp_name']
$uploaded->set_tmp_name($_FILES['file']['tmp_name']);
// Set file size,
$uploaded->set_file_size($_FILES['file']['size']);
// Set File Type,
$uploaded->set_file_type($_FILES['file']['type']);
// Set File Name,
$uploaded->set_file_name($_FILES['file']['name']);
// Start Copy Process
$uploaded->start_copy();
// Control File is uploaded or not
// If there is error write the error message
if($uploaded->is_ok()){
echo "successfully loaded <br />";
}else{
echo $uploaded->error()."<br>";
}this should show only file name but it does not.
为什么你所期望的类包含在成员变量名?你在哪里分配给成员变量?我看到的只是你正在创建一个新类(除此之外,它应该是“new upload();”,你错过了括号),其成员变量没有初始化。所以你在做回声时会得到一个空值,这是预期的结果。 你想达到什么目的?如果您希望类实例在不同的请求 - 响应周期内“保留”它的值,则必须将整个实例存储在某处(将其分散),并在需要时对其进行恢复(将其反序列化)。如果您需要的话,您也可以简单地将文件名存储在会话中。
你不需要有'()声明所有对象时。 http://ideone.com/6Jnhc – 2011-05-28 18:15:39
@Jared Farrish,特别是,当构造函数不需要任何参数时,它们可能会被省略。 – erisco 2011-05-28 18:48:45
@erisco - 感谢您澄清方法调用需要使用括号。 ;) – 2011-05-28 18:53:16
我不确定你想达到什么,但是就目前得到的错误而言......你需要为Upload
类创建一个构造函数,该类接受$user_file_name
的值,然后设置它。或者,您可以在尝试使用$user_file_name
变种之前使用set_file_name()
。目前的情况是,价值永远不会被设置,这就是为什么你在致电echo
时遇到错误。
另外,正如其他人所说,如果您发现它们有帮助,您应该回头接受问题的答案。
你需要这是奇怪的,我做了评论,并发出了感谢信息我的每个问题被回答。我将再次检查 – mary 2011-05-28 18:32:00
主要的是好的IDE,随着智能代码高亮:)
我建议PhpStorm(并不理想,但最好在这一刻,我希望有人将创造更好的东西)。
在您的代码$ userfile没有定义。您可以定义这个变量:$userfile = $this->user_tmp_name;
in函数start_copy()
。
在echo声明之后你真的没有';'吗? – 2011-05-28 18:09:01
我也怀疑这一点。 – ralphtheninja 2011-05-28 18:09:26
ops对不起,有;回声后,但仍然我得到这个错误味精致命错误:无法访问/ home/test/domain/skyup.nl/public_html/upload_Class/upload.php上线69空的属性 – mary 2011-05-28 18:10:36