Calling a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mavedog21
Forum Newbie
Posts: 23
Joined: Thu Dec 21, 2006 1:27 pm

Calling a class

Post by mavedog21 »

Hey all,

I've been working on a tutorial from phpFreaks.com and have a problem with my files working together.

Here's the set-up:

File 1 : HTML Form with a File upload (works fine)

File 2: myUpload_execute.php (believe this should collect my variables from the form and create a new class)

File 3: myUpload_class.php (file that has all my information on what to do)

Problem:
I receive this error.

Code: Select all

Fatal error: Cannot instantiate non-existent class: upload_files in /home/wildiris/public_html/phpTest/myUpload_execute.php on line 8
*note upLoad_files is the name of the new class.

Any suggestions:

Here are links to the files.
File 1: http://www.wildirishcustoms.com/phpTest/myUpload.htm (the html page)

File 2: (collects variables / "includes" class)

Code: Select all

<?php
############### Calling the Class ###########################################################
//SECTION #1 
include("myUpload_class.php");
//This line is very important, it tells php to load your external class file, Make sure this is the right name and location!


$upload_class = new Upload_Files; 
$upload_class->temp_file_name = trim($_FILES['upload']['tmp_name']); 
$upload_class->file_name = trim(strtolower($_FILES['upload']['name'])); 
$upload_class->upload_dir = "images/"; 
$upload_class->upload_log_dir = "images/"; 
$upload_class->max_file_size = 5242880; 
$upload_class->banned_array = array(""); 
$upload_class->ext_array = array(".jpeg",".jpg",".gif",".png"); 

$valid_ext = $upload_class->validate_extension(); 
$valid_size = $upload_class->validate_size(); 
$valid_user = $upload_class->validate_user(); 
$max_size = $upload_class->get_max_size(); 
$file_size = $upload_class->get_file_size(); 
$file_exists = $upload_class->existing_file(); 

    if (!$valid_ext) { 
        $result = "The file extension is invalid, please try again!"; 
    } 
    elseif (!$valid_size) { 
        $result = "The file size is invalid, please try again! The maximum file size is: $max_size and your file was: $file_size"; 
    } 
    elseif (!$valid_user) { 
        $result = "You have been banned from uploading to this server."; 
    } 
    elseif ($file_exists) { 
        $result = "This file already exists on the server, please try again."; 
    } else { 
        $upload_file = $upload_class->upload_file_with_validation(); 
        if (!$upload_file) { 
            $result = "Your file could not be uploaded!"; 
        } else { 
            $result = "Your file has been successfully uploaded to the server."; 
        } 
    } 
?>

File 3: (my class)

Code: Select all

<?php 
class Upload_File{


var $temp_file_name; 
var $file_name; 
var $upload_dir; 
var $upload_log_dir; 
var $max_file_size; 
var $banned_array; 
var $ext_array;


#################### Validate Extiension ############################

function validate_extension() { 


    //SECTION #1 
    $file_name = trim($this->file_name); 
    $extension = strtolower(strrchr($file_name,".")); 
    $ext_array = $this->ext_array; 
    $ext_count = count($ext_array); 
	
	

	
	
	

    //SECTION #2 
    if (!$file_name) { 
        return false; 
		
    } else { 
        if (!$ext_array) { 
            return true; 
        } else { 
            foreach ($ext_array as $value) { 
                $first_char = substr($value,0,1); 
                    if ($first_char <> ".") { 
                        $extensions[] = ".".strtolower($value); 
                    } else { 
                        $extensions[] = strtolower($value); 
                    } 
            } 

            //SECTION #3 
            foreach ($extensions as $value) { 
                if ($value == $extension) { 
                    $valid_extension = "TRUE"; 
                }                 
            } 

            //SECTION #4 
            if ($valid_extension) { 
                return true; 
            } else { 
                return false; 
            } 
        } 
    } 
} 

################ Validate Size ###################################

function validate_size() { 



    $temp_file_name = trim($this->temp_file_name); 
    $max_file_size = trim($this->max_file_size); 




    if (!$temp_file_name) { 
        $size = filesize($temp_file_name); 
            if ($size > $max_file_size) { 
                return false;                                                         
            } else { 
                return true; 
            } 
    } else { 
        return false; 
    }     
} 

############### Validate if file exists ##########################

function existing_file() { 



    $file_name = trim($this->file_name); 
    $upload_dir = $this->get_upload_directory(); 





    if ($upload_dir == "ERROR") { 
        return true; 
    } else { 
        $file = $upload_dir . $file_name; 
        if (file_exists($file)) { 
            return true; 
        } else { 
            return false; 
        } 
    }     
} 

################### get_file size #################################

function get_file_size() { 

    //SECTION #1 
    $temp_file_name = trim($this->temp_file_name); 
    $kb = 1024; 
    $mb = 1024 * $kb; 
    $gb = 1024 * $mb; 
    $tb = 1024 * $gb; 

        //SECTION #2 
        if ($temp_file_name) { 
            $size = filesize($temp_file_name); 
            if ($size < $kb) { 
                $file_size = "$size Bytes"; 
            } 
            elseif ($size < $mb) { 
                $final = round($size/$kb,2); 
                $file_size = "$final KB"; 
            } 
            elseif ($size < $gb) { 
                $final = round($size/$mb,2); 
                $file_size = "$final MB"; 
            } 
            elseif($size < $tb) { 
                $final = round($size/$gb,2); 
                $file_size = "$final GB"; 
            } else { 
                $final = round($size/$tb,2); 
                $file_size = "$final TB"; 
            } 
        } else { 
            $file_size = "ERROR: NO FILE PASSED TO get_file_size()"; 
        } 
        return $file_size; 
} 

############## get_ max_ size of file being uploaded ##################

function get_max_size() { 


    $max_file_size = trim($this->max_file_size); 
    $kb = 1024; 
    $mb = 1024 * $kb; 
    $gb = 1024 * $mb; 
    $tb = 1024 * $gb; 

    if ($max_file_size) { 
        if ($max_file_size < $kb) { 
            $max_file_size = "max_file_size Bytes"; 
        } 
        elseif ($max_file_size < $mb) { 
            $final = round($max_file_size/$kb,2); 
            $max_file_size = "$final KB"; 
        } 
        elseif ($max_file_size < $gb) { 
            $final = round($max_file_size/$mb,2); 
            $max_file_size = "$final MB"; 
        } 
        elseif($max_file_size < $tb) { 
            $final = round($max_file_size/$gb,2); 
                $max_file_size = "$final GB"; 
        } else { 
            $final = round($max_file_size/$tb,2); 
            $max_file_size = "$final TB"; 
        } 
    } else { 
        $max_file_size = "ERROR: NO SIZE PARAMETER PASSED TO  get_max_size()"; 
    } 
        return $max_file_size; 
} 

############### Validate User is able to upload #################################

function validate_user() { 


    //SECTION #1 
    $banned_array = $this->banned_array; 
    $ip = trim($_SERVER['REMOTE_ADDR']); 
    $cpu = gethostbyaddr($ip); 
    $count = count($banned_array); 

    //SECTION #2 
    if ($count < 1) { 
        return true; 
    } else { 
        foreach($banned_array as $key => $value) { 
            if ($value == $ip ."-". $cpu) { 
                return false; 
            } else { 
                return true; 
            } 
        } 
    } 
} 

############## get_upload directory ###############################################

function get_upload_directory() { 


    //SECTION #1 
    $upload_dir = trim($this->upload_dir); 

    //SECTION #2 
    if ($upload_dir) { 
        $ud_len = strlen($upload_dir); 
        $last_slash = substr($upload_dir,$ud_len-1,1); 
            if ($last_slash <> "/") { 
                $upload_dir = $upload_dir."/"; 
            } else { 
                    $upload_dir = $upload_dir; 
            } 

        //SECTION #3 
        $handle = @opendir($upload_dir); 
            if ($handle) { 
                $upload_dir = $upload_dir; 
                closedir($handle); 
            } else { 
                $upload_dir = "ERROR"; 
            } 
    } else { 
        $upload_dir = "ERROR"; 
    } 
    return $upload_dir; 
} 

################### get_upload_log_directory ############################

function get_upload_log_directory() { 


    $upload_log_dir = trim($this->upload_log_dir); 
    if ($upload_log_dir) { 
        $ud_len = strlen($upload_log_dir); 
        $last_slash = substr($upload_log_dir,$ud_len-1,1); 
            if ($last_slash <> "/") { 
                $upload_log_dir = $upload_log_dir."/"; 
            } else { 
                $upload_log_dir = $upload_log_dir; 
            } 
            $handle = @opendir($upload_log_dir); 
                if ($handle) { 
                    $upload_log_dir = $upload_log_dir; 
                    closedir($handle); 
                } else { 
                    $upload_log_dir = "ERROR"; 
                } 
    } else { 
        $upload_log_dir = "ERROR"; 
    } 
    return $upload_log_dir; 
} 

################ upload_file_no_validation ##############################

function upload_file_no_validation() { 



    //SECTION #1 
    $temp_file_name = trim($this->temp_file_name); 
    $file_name = trim(strtolower($this->file_name)); 
    $upload_dir = $this->get_upload_directory(); 
    $upload_log_dir = $this->get_upload_log_directory(); 
    $file_size = $this->get_file_size(); 
    $ip = trim($_SERVER['REMOTE_ADDR']); 
    $cpu = gethostbyaddr($ip); 
    $m = date("m"); 
    $d = date("d"); 
    $y = date("Y"); 
    $date = date("m/d/Y"); 
    $time = date("h:i:s A"); 

    //SECTION #2 
    if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) { 
        return false; 
    } else { 
        if (is_uploaded_file($temp_file_name)) { 
            if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) { 
                $log = $upload_log_dir.$y."_".$m."_".$d.".txt"; 
                $fp = fopen($log,"a+"); 
                fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time"); 
                fclose($fp); 
                return true; 
            } else { 
                return false;     
            } 
        } else { 
            return false; 
        } 
    } 
} 

#################### upload_file_with_validation ##################################

function upload_file_with_validation() { 



    //SECTION #1 
    $temp_file_name = trim($this->temp_file_name); 
    $file_name = trim(strtolower($this->file_name)); 
    $upload_dir = $this->get_upload_directory(); 
    $upload_log_dir = $this->get_upload_log_directory(); 
    $file_size = $this->get_file_size(); 
    $ip = trim($_SERVER['REMOTE_ADDR']); 
    $cpu = gethostbyaddr($ip); 
    $m = date("m"); 
    $d = date("d"); 
    $y = date("Y"); 
    $date = date("m/d/Y"); 
    $time = date("h:i:s A"); 
    $existing_file = $this->existing_file();    //<-Add On 
    $valid_user = $this->validate_user();        //<-Add On 
    $valid_size = $this->validate_size();        //<-Add On 
    $valid_ext = $this->validate_extension();    //<-Add On 

    //SECTION #2 
    if (($upload_dir == "ERROR") OR ($upload_log_dir == "ERROR")) { 
        return false; 
		echo "error on upload directory";
    } 
    elseif ((((!$valid_user) OR (!$valid_size) OR (!$valid_ext) OR ($existing_file)))) { 
        return false; 
		echo "error on validating user, size, ext or exisiting file";
    } else { 
        if (is_uploaded_file($temp_file_name)) { 
            if (move_uploaded_file($temp_file_name,$upload_dir . $file_name)) { 
			echo "uploading";
                $log = $upload_log_dir.$y."_".$m."_".$d.".txt"; 
                $fp = fopen($log,"a+"); 
                fwrite($fp,"$ip-$cpu | $file_name | $file_size | $date | $time"); 
                fclose($fp); 
                return true; 
            } else { 
                return false; 
            } 
        } else { 
            return false; 
        } 
    } 

  
  } 
}

?>
any ideas? Am I far off? It seems to not want to read the class Upload_Files.

please help,

sky
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Calling a class

Post by volka »

plural:
mavedog21 wrote:$upload_class = new Upload_Files;
singular:
mavedog21 wrote:class Upload_File
mavedog21
Forum Newbie
Posts: 23
Joined: Thu Dec 21, 2006 1:27 pm

Post by mavedog21 »

Thanks for that. I seemed to read it to quickly.

Now that's fixed but it's not uploading. Can you see anywhere in the code any blantly obvious
errors as to why it would not upload my file?

if not, thanks for the help.

sky
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do you have any error checking going on? That would tell you a bunch.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
############### Calling the Class ###########################################################
//SECTION #1
error_reporting(E_ALL); ini_set('display_errors', true);
require "myUpload_class.php";
//This line is very important, it tells php to load your external class file, Make sure this is the right name and location!
...
mavedog21
Forum Newbie
Posts: 23
Joined: Thu Dec 21, 2006 1:27 pm

Post by mavedog21 »

When I select my file and hit submit it goes to a blank page.
Even with that snippet of code. Nothing appears to happen.

I know it's running through all my functions. It's just not
uploading the gosh darn file. :?

Well try again tomorrow. Day 5, thank goodness I like this stuff.

any other ideas on how to check it?
thanks for all your help.

sky
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you are getting a blank screen then you are getting a parse error, which the ini_set directives will not reveal. Your best bet is the check your server error logs to see where the parse error is, or turn on display_errors in the php.ini file (which I do NOT recommend for production systems, though if you are developing, you should technically be on a development system :wink:).
mavedog21
Forum Newbie
Posts: 23
Joined: Thu Dec 21, 2006 1:27 pm

Post by mavedog21 »

Thanks Everah,

I've turned on my error log and it takes 24 hours to start.
Just want to say thanks for the help to both of you guys.

sky
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

24 hours? What kind of error log is that?

Anyway, you're welcome. Post back when there is more information. On a side note, the error logging should be already taking place by apache, so a tail of the error log for your server should be able to provide some assistance, if your host is willing to provide that information to you.
Post Reply