Welcome Guest, Not a member yet? Register   Sign In
File upload validation
#3

[eluser]macleodjb[/eluser]
I'm working on a file upload script but it's crapping out somewhere i can't figure out where, I seem to have a problem with getting my error messages to show up when something fails, this worked fine before integrating with CI.

I have all of this code inside the controller.
Code:
<?php
class File extends Controller {

    function File()
    {
        parent::Controller();
        session_start();
    }
    
    function index() {
        redirect('dashboard');        
    }// end of function
    
    function add_file_to_job() {
        //$this->load->model('File_model');
        $this->load->model('Main_model');
        $this->load->library('form_validation');    

        $this->form_validation->set_rules('user_file', 'File', 'required');        

        $job_id = $this->uri->segment(3);
        if ($this->form_validation->run() == FALSE){
            $this->load->view('file/upload_form', $data);
        } else {
            $file_types = array('pdf', 'zip');
            $max_size = '4096';
            $path = 'files/job/';
            if($this->upload_file($file_types, $max_size, $path, $job_id)){
            $this->load->view('file/upload_success', $data);
            } else {
            $data['error'] = "There was an internal error, you file was not saved, Please contact support";
            $this->load->view('file/upload_form', $data);
            }
        }
        
    }// end of function
    
    
    function checkFileProperties($file_types, $max_size) {
        $file_basename = strtolower(basename($_FILES['user_file']['name']));
        if($this->checkFileExt($file_basename,$file_types)) {
            if($this->checkFileSize($file_basename,$max_size)) {
            return true;
            } else {//else for check file size
            return false;
            }
        } else {//else for check file ext
        return false;
        }
    }
    
    function createFileName($extension,$user_id) {
        $random = mt_rand();
        $newfilename = $user_id . $random . "." . $extension;
        return $newfilename;
    }
    
    function SaveFileToDB($uid, $name, $oldname, $path, $job_id = '', $profile_photo = '', $company_photo = '', $portfolio_photo = '') {
      $save_query = $this->db->query("INSERT INTO files (job_id, file_name, old_filename, user_id, path, company_photo, portfolio_photo, profile_photo) VALUES ('".$job_id."', '".$name."', '".$oldname."', '".$uid."', '".$path."', '".$company_photo."', '".$portfolio_photo."', '".$profile_photo."' )");
    $saved = $this->db->affected_rows($save_query);
        if($saved > 0){
        return true;
        } else {
        return false;
        }
    }
    function uploadFile($file_types, $max_size, $path, $job_id) {
        if($this->checkFileProperties($file_types, $max_size)) {
            $extension = end(explode('.', strtolower(basename($_FILES['user_file']['name']))));
            $name = $this->createFileName($extension,$_SESSION['uid']);
            $newfilename = $path . $name;
            move_uploaded_file($_FILES['user_file']['tmp_name'], $newfilename);
            @chmod($newfilename, 0644);
            $oldname = basename($_FILES['user_file']['name']);
            if($this->SaveFileToDB($_SESSION['uid'], $name, $oldname, $path, $job_id)){
            return true;
            } else {
            return $error = "ERROR: We had some trouble saving your file to the database, please contact support";
            //return false;
            }
        } else {
        $error = "ERROR: The file you are trying to upload has invalid properties, Please check the file requirements on this page";
        }
    }
    
    function checkFileExt($filename,$file_types) {
    $extension = end(explode('.', $filename));
        if(in_array($extension,$file_types)) {
        return true;
        } else {
        return false;
        }
    }
    
    function checkFileSize($filename,$max_size) {
    $filesize = @filesize($filename);
        if($filesize < $max_size) {
        return true;
        } else {
        return false;
        }
    }

    
    
}?&gt;


Messages In This Thread
File upload validation - by El Forum - 02-23-2009, 05:24 PM
File upload validation - by El Forum - 02-23-2009, 05:37 PM
File upload validation - by El Forum - 02-24-2009, 06:36 PM
File upload validation - by El Forum - 02-24-2009, 08:20 PM
File upload validation - by El Forum - 02-25-2009, 05:18 AM
File upload validation - by El Forum - 02-25-2009, 05:36 AM
File upload validation - by El Forum - 02-25-2009, 05:40 AM
File upload validation - by El Forum - 02-25-2009, 06:14 PM
File upload validation - by El Forum - 03-22-2009, 11:40 AM
File upload validation - by El Forum - 03-22-2009, 12:23 PM
File upload validation - by El Forum - 03-22-2009, 12:29 PM
File upload validation - by El Forum - 03-22-2009, 03:33 PM
File upload validation - by El Forum - 03-22-2009, 04:07 PM
File upload validation - by El Forum - 03-22-2009, 04:31 PM
File upload validation - by El Forum - 03-22-2009, 04:52 PM
File upload validation - by El Forum - 03-22-2009, 05:00 PM
File upload validation - by El Forum - 03-22-2009, 05:06 PM
File upload validation - by El Forum - 03-28-2009, 09:32 AM
File upload validation - by El Forum - 03-28-2009, 09:37 AM
File upload validation - by El Forum - 03-28-2009, 10:50 AM
File upload validation - by El Forum - 04-11-2009, 11:31 AM
File upload validation - by El Forum - 04-11-2009, 11:44 AM
File upload validation - by El Forum - 04-11-2009, 12:09 PM
File upload validation - by El Forum - 04-11-2009, 12:19 PM
File upload validation - by El Forum - 04-11-2009, 12:33 PM
File upload validation - by El Forum - 04-11-2009, 05:39 PM
File upload validation - by El Forum - 04-12-2009, 02:41 AM
File upload validation - by El Forum - 04-12-2009, 05:08 AM
File upload validation - by El Forum - 04-12-2009, 08:17 AM
File upload validation - by El Forum - 04-12-2009, 10:50 AM
File upload validation - by El Forum - 10-23-2009, 10:09 AM
File upload validation - by El Forum - 10-23-2009, 10:44 AM
File upload validation - by El Forum - 12-23-2010, 10:56 PM
File upload validation - by El Forum - 02-27-2011, 11:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB