file upload html and php code example

Example: php upload

// To change: FILENAME, array with allowed extensions, Max Filesite, Filepath
if(upload("FILENAME", array("jpeg","jpg","png"), 209715, "C:/xampp/htdocs/")){
        echo "Success";
    }


function upload($f_name, $f_ext_allowed, $f_maxsize, $f_path){

      $f_name_2 = $_FILES[$f_name]['name'];
      $f_size  =  $_FILES[$f_name]['size'];
      $f_tmp   =  $_FILES[$f_name]['tmp_name'];
      $f_error =  $_FILES[$f_name]['error'];
      $f_ext   = strtolower(end(explode('.',$f_name_2)));
      $f_rename = $_SESSION['uid'] . "." . $f_ext;

        if($f_error == 0 && in_array($f_ext, $f_ext_allowed) 
        && $f_size < $f_maxsize && mb_strlen($f_name_2, "UTF-8") < 225 
        && preg_match("`^[-0-9A-Z_\.]+$`i", $f_name_2)){
            if(move_uploaded_file($f_tmp, $f_path . $f_name_2){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
}

Tags:

Php Example