开发者

How can i get variable out of a class?

开发者 https://www.devze.com 2023-03-09 12:52 出处:网络
I am trying to get variable out of a class but seems that I am not doing it correctly. Here is code for upload_inc.php

I am trying to get variable out of a class but seems that I am not doing it correctly.

Here is code for 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;
        }
    }

}

In the second page after uploading the file, I am trying to get only the file name. Then, I can store the file name in my database. Here is my code.

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.


Why do you expect the class to contain the filename in the member variable? Where are you assigning it to the member variable? All I see is that you are creating a new class (besides, it should be "new upload();", you missed the parentheses) whose member variables are not initalized. So you get a null value when doing the echo, which is the expected result. What do you try to achieve? If you want the class-instance to "keep" its values across different request-response cycles you would have to store the whole instance somewhere (seralizing it) and restoring it when needed (unserializing it). You could also simply store the filename in the session if that is all you need.


I'm not sure what you're trying to achieve but as far as the error you're currently getting... You'll need to create a constructor for your Upload class that accepts a value for $user_file_name and then set it. Alternatively you can use set_file_name() before trying to use the $user_file_name var. As it stands right now that value is never being set, which is why you're getting an error on your call to echo.

Also, as others have said, you should really go back and accept answers to your questions if you found them helpful.


Main thing which you need is good IDE, with smart code highlighting :)
I recommend PhpStorm (not ideal, but best at this moment, I hope somebody will create something better).

In your code $userfile are not defined. You can define this variable:
$userfile = $this->user_tmp_name;
in function start_copy().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号