我试图从类中变量,但似乎我没有正确地做到这一点。
以下是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.
答案 0 :(得分:3)
为什么期望该类在成员变量中包含文件名?你在哪里将它分配给成员变量?我只看到你正在创建一个新类(此外,它应该是“new upload();”,你错过了括号),其成员变量不是初始化的。因此,在执行echo时会得到一个空值,这是预期的结果。 你想做什么?如果您希望类实例在不同的请求 - 响应周期中“保持”其值,则必须将整个实例存储在某处(将其分离)并在需要时将其还原(反序列化)。如果这就是你所需要的,你也可以简单地将文件名存储在会话中。
答案 1 :(得分:1)
我不确定你要实现的是什么,但就你目前得到的错误而言......你需要为Upload
类创建一个接受一个值的构造函数$user_file_name
然后设置它。或者,您可以在尝试使用set_file_name()
var之前使用$user_file_name
。现在看来,价值永远不会被设置,这就是您拨打echo
时出错的原因。
此外,正如其他人所说,如果您发现问题的答案,您应该回过头来接受问题的答案。
答案 2 :(得分:0)
你需要的主要是好的IDE,智能代码突出显示:)
我推荐PhpStorm(不理想,但此时最好,我希望有人会创造更好的东西)。
在您的代码中,$ userfile未定义。您可以定义此变量:
$userfile = $this->user_tmp_name;
在函数start_copy()
中。