PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Stefan Jibrail Froelich   Uploader Class   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: contains examples and explanations of the various methods and how to use them
Class: Uploader Class
process and upload files
Author: By
Last change: Changed priority
Date: 12 years ago
Size: 5,305 bytes
 

Contents

Class file image Download
<?php

# This file contains tutorials and examples on how to use the uploader class.



   
    ###########################################################
    ################### Uploading a file from a form ##########
    ###########################################################


# include class file
   
require('uploader.php');
   
# echo the upload form
   
echo '
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="input_name_of_file_to_be_uploaded">
        <input type="submit" name="upload" value="Upload">
    </form>'
;

# check if file has been submitted
if (isset($_POST['upload'])){

# Instantiate a new uploader object.
# Set upload directory to current path
# Set debug to silent. Errors will not be diplayed. Other options are UPLOADER::ERROR to display errors; UPLOADER::EXCEPTION to throw exceptions
# The third parameter is set to form by default and can be ignored, change to 'ajax' to upload via ajax
# All parameters are optional. Defaults are ('./', UPLOADER::ERROR, 'form')
   
$uploader = new uploader( './', UPLOADER::SILENT, 'form' );
   
# You can also change the error mode anytime by passing your desired error mode to the setErrorMode() method
   
$uploader->setErrorMode( UPLOADER::ERROR );
   
   
# set the a whitelist of file extensions that can be uploaded
   
$allowed_types = array('.png', '.gif', '.jpeg');
   
# set max size as 2MB. sizes should be in megabytes
# All limit values are optional. You can pass an empty array if you wish
# The max_len and min_len values are for the max and minimum name length
   
$limits = array('max_size' => 2, 'min_size' => '', 'max_len' => '', 'min_len' => '' );
   
# Set the name of the form input. It is the only required parameter
# This method returns an array of the file name, base(name without extension), ext(extension), and size ) if file passes the check
# and false if it fails
# You can run this in a loop on different form fields to upload multiple files
   
$check = $uploader->checkFile('input_name_of_file_to_be_uploaded', $allowed_types, $limits );

   
   
###########################################################
    ################## Uploading a file through ajax ##########
    ###########################################################


# include class file
   
require('uploader.php');
   
   
# Instantiate a new uploader object.
# Third parameter changes to 'ajax'
   
$uploader = new uploader( './', UPLOADER::SILENT, 'ajax' );
   
# You can also change the error mode anytime by passing your desired error mode to the setErrorMode() method
   
$uploader->setErrorMode( UPLOADER::ERROR );

# Limit specifications remains the same
   
$allowed_types = array('.png', '.gif', '.jpeg');
   
$limits = array('max_size' => 2, 'min_size' => '', 'max_len' => '', 'min_len' => '' );
   
# The first parameter changes here
# Depending on your clientside javascript, you need to pass the file name to the the method
# It might be passed through the query string. eg $_GET['filename']
# The method checks first for the existence of the $_SERVER['HTTP_X_FILE_NAME'] and uses the passed filename if it does not exist
   
$check = $uploader->checkFile($_GET['filename'], $allowed_types, $limits );


   
###########################################################
    ################### The various upload methods ##########
    ###########################################################
   
# The methods below are the same for both ajax and form uploads there are no differences in the way they are called

   
if ( $check )
    {
   
# Upload a file in zip format
# first and second parameters are to override the filename and upload directory if not empty
# This allows you to upload files to different directories on the fly
# .zip is appended to the filename
# The third parameter determines if the original file should be kept as well or removed
# It returns an array of the filename(zipped), filepath(zipped) and if 3rd param is true, original_filename and original filepath on success
# and false on failure
# This requires you to have PclZip in the same directory
       
if ( !$uploader->uploadZipped( '', '', true ) ){
           
var_dump( $uploader->getError() );
        }
       
uploadFile( $filename = '', $upload_dir = '', $strict = false, $thumbnail = array( 0 => false, 'width' => 64,'height' => 64), $filetype = '' ){

# First and second parameters are to override the filename and upload directory if not empty
# Third param if set to true, will remove exif data using GD functions if file is an image. Certain images maybe renamed with a .jpg extension
# Fourth param will upload the file image resized if it is an image. Script will determine correct ratio and upload file as such.
# Notice that strict mode is set to false. If thumbnail mode and strict are set to true, thumbnail mode takes precedence.
# Only files marked as image in the file_mime array are affected by image checks and functions since these are the commonly supported files
# on GD
# The method returns an array of filename and filepath on success and false on failure
       
if ( !$uploader->uploadFile( '', '', false, array(true, 'width' => 64, 'height' => 64) )){
           
var_dump( $uploader->getError() );
        }
    }
    else
var_dump( $uploader->getError() );
   
}





?>