Pages

Sunday, July 8, 2012

A file upload helpful php code

if(isset($_POST['sendmail']))
{
    //echo "
";
    //print_r($_POST);
    //print_r($_FILES);
    //echo "
";

    //$attachment = chunk_split(base64_encode(file_get_contents($_POST['upload'])));

    //Get the uploaded file information
    $name_of_uploaded_file =
    basename($_FILES['upload']['name']);
    //echo $name_of_uploaded_file;
    //get the file extension of the file
    $type_of_uploaded_file =substr($name_of_uploaded_file,strrpos($name_of_uploaded_file, '.') + 1);
    //echo $type_of_uploaded_file;
    $size_of_uploaded_file =$_FILES["upload"]["size"]/1024;
    //echo $size_of_uploaded_file;
    $max_allowed_file_size = 100; // size in KB
    $allowed_extensions = array("doc", "pdf", "docx","txt"winking smiley;
    //Validations
    if($size_of_uploaded_file > $max_allowed_file_size )
    {
        $errors .= "\n Size of file should be less than $max_allowed_file_size";
    }
    //------ Validate the file extension -----
    $allowed_ext = false;
    for($i=0; $i    {
        if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
        {
            $allowed_ext = true;
        }
    }
    if(!$allowed_ext)
    {
        $errors .=
        " Only the following file types are supported: ".implode(',',$allowed_extensions);
    }

    if(!isset($errors))
    {
        move_uploaded_file($_FILES["upload"]["tmp_name"],WP_CONTENT_DIR.'/uploads/'.basename($_FILES['upload']['name']));
        $to=  get_option('ugmm_career_email');
        $subject=get_option('ugmm_career_upload_field');
        $message="File uploaded by".get_option('ugmm_career_upload_field');
        $headers = 'From:upload<'.$to.'>';
        $attachments = array(WP_CONTENT_DIR ."/uploads/".$_FILES["upload"]["name"]);
        wp_mail($to,$subject,$message,$headers,$attachments);
        $sentmail=true;
    }
    else
    $sentmail=false;
}
?>

No comments:

Post a Comment