Welcome to the Geeks & God Static Archive. Read more »

file upload class minor issues ;)

Joined: 11/28/2008

** solved **

haha, wow, that has taken me about 3 hours to fix, and all for the sake of a $ in the wrong place!!! the following line:

CODE
$file_name = trim($this->$file_name);

should have been

CODE
$file_name = trim($this->file_name);

for anyone interested /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" />

** solved **

hello all,
i have a php class that uploads files, which i found as a tutorial on the net. this is the first time i have used it, and so far, just 2 small errors seem to happen.. it isn't accepting the extensions it should, and it is always saying that the filesize is too big.. the extensions is the main validation i want to check, and the function thaty does this in the class is as follows:

CODE
function validate_extension() {
      //set vars
      $file_name = trim($this->$file_name);
      $extension = strtolower(strchr($file_name, "."));
      $ext_array = $this->ext_array;
      $ext_count = count($ext_array);
      
      //start validation process
      if(!$file_name) {
        return false;
      }
      else {
        if(!$ext_array) {
          return true;
        }
        else {
          //put extensions into an array
          foreach($ext_array as $value) {
            $first_char = substr($value, 0, 1);
            if($first_char <> ".") {
              $extensions[] = "." . strtolower($value);
            }
            else {
              $extensions[] = strtolower($value);
            }
          }
          
          //find out if the extension is valid
          foreach($extensions as $value) {
            if($value == $extension) {
              $valid_extension = "TRUE";
            }
          }
          
          //is the $valid_extension variable present?
          if($valid_extension) {
            return true;
          }
          else {
            return false;
          }
        }
      }
    }

so, if it returns false, the extension is not valid and it should flag an error... to call it, I have:

CODE
$upload_class = new Upload_Files;
                $upload_class->temp_file_name = trim($_FILES['upload']['tmp_name']);
                $upload_class->file_name = trim($_SESSION['uid'] . strtolower($_FILES['upload']['name']));
                $upload_class->upload_dir = "images/";
                $upload_class->upload_log_dir = "images/upload_logs/";
                $upload_class->max_file_size = 524288;
                $upload_class->banned_array = array("");
                $upload_class->ext_array = array(".jpg", ".gif", ".jpeg");
                
                //run through class functions and upload image.
                $valid_ext = $upload_class->validate_extension();
                $valid_size = $upload_class->validate_size();
                //$validate_user = $upload_class->validate_user();
                $max_size = $upload_class->get_max_size();
                $file_size = $upload_class->get_file_size();
                $upload_directory= $upload_class->get_upload_directory();
                $upload_log_directory = $upload_class->get_upload_log_directory();
                $upload_file = $upload_class->upload_file_with_validation();

so it should accept all 3 extensions... can anyone see what i am doing wrong here?

thanks

Patrick

Joined: 11/28/2008
Thanks for posting the

Thanks for posting the solution and leaving the original probelm here Patrick! /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />

Paul Davey
Whitford Church
"Everyone who calls on the name of the Lord will be saved." Romans 10:13
"For all have sinned and fall short of the glory of God, and are justified