This is a very userful php upload script in the form of flexible, simple and powerful PHP upload class. This class is meant to validate and manage files uploaded via Web forms.
It is the ideal class to quickly integrate file upload and manipulation in your site. This script will allow you to upload files from your browser to your hosting, using PHP.
Features
Uploads any image / file
File name extension validation to accept only files with a given list of extensions
Check whether the files exceeds the given size limit
Safely renames the file – removes spaces, special characters etc
Options to overwrite or make unique
File extension validation
Flexible error reporting system
Check if a directory and/or file exist
Chip PHP upload class allows you to select and upload multiple files at once rather than having to select and upload each file individually.
Requirements
PHP5 or higher
Learning and handling of PHP upload class by TutorialChip is very easy and flexible. I have also given a complete demo and usage in the download archive for your understanding, and i am going to explain the installation of this class step by step online as well.
Client Side Tutorial: File Upload HTML Form
It is the very important step to prepare the form for uploading files.
First important thing is to set "enctype" attribute in the form tag. i.e enctype="multipart/form-data"
PHP Upload Class is very powerful as it can handle "Single/Multiple" uploads easily, So you have to define the file input as an array i.e upload_file[]. You can view in the given code snippet.
File Upload Form: Single File Example
<form method="post" action="" enctype="multipart/form-data">
<p>Upload File: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<input type="submit" name="submit" value="Upload File" />
</form>
File Upload Form: Multiple File Example
<form method="post" action="" enctype="multipart/form-data">
<p>Upload File 1: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 2: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 3: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 4: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 5: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<input type="submit" name="submit" value="Upload File" />
</form>
Server Side Tutorial
As we have already prepared our form to send data to the PHP server to upload files. Now it is time to learn PHP upload class handling to complete this process smoothly.Auto Detection of Script Directory
PHP Upload Class script will try to detect the her directory path automatically by built-in Magic Constant of PHP i.e __DIR__. However you can alter it according to your need.
/*
|-----------------
| Chip Constant Manipulation
|------------------
*/
define( "CHIP_DEMO_FSROOT", __DIR__ . "/" );
Include "class.chip_upload.php"
You have to include PHP upload file in your file at the suitable position you think. I will recommend to include this class in your POST block. Don’t worry about POST block, you can easily understand it as the tutorial moves and from a descriptive demo in the download archive.
/*
|-----------------
| Chip Upload Class
|------------------
*/
require_once("class.chip_upload.php");
Upload Directory Path
Next step is to define upload directory path. This is the directory where you want to save your uploaded files. I have defined it "uploads", which is a directory in the download archive. Make sure that this directory should be writable.
/*
|-----------------
| Upload(s) Directory
|------------------
*/
$upload_directory = CHIP_DEMO_FSROOT . "uploads/";
Class Instance
It is time to make instance of PHP upload class now.
/*
|-----------------
| Class Instance
|------------------
*/
$object = new chip_upload();
$_FILES Manipulation
Our form will send files in the "upload_file" array which is packed in $_FILES. Our class is very powerful to handle Single and Multiple files efficiently, so we will pass this array to the "get_upload_var" method which will arrange this array for next processing professionally.
/*
|-----------------
| $_FILES Manipulation
|------------------
*/
$files = $object->get_upload_var( $_FILES['upload_file'] );
Powerful Upload Loop
Now we will start loop to upload files one by one. Let’s dissect this loop part by part. We are going to provide "$files" array to this loop.
/*
|-----------------
| Upload File
|------------------
*/
foreach( $files as $file ) {
Powerful Upload Loop
It is time to prepare inputs for class operation. Chip Upload class will take two parameters,
Array: $args
upload_file (Array): It will hold uploaded file array. You have to simply pass $file to this array as we are dissecting foreach loop on $file variable.
upload_directory (String – Correct Absolute Path): You have to provide a correct absolute path to this variable. We have already calculated this variable as $upload_directory
allowed_size (Integer – In Bytes): You have to provide maximum upload size to this part. You have to given size in bytes. 1Kb = 1024 bytes
extension_check (Boolean – TRUE | FALSE): This part will allow you to control over specific files to upload.
upload_overwrite (Boolean – TRUE | FALSE): This part will allow you to control for over-writing existing file or not. If set False, PHP upload class will automatically determine new name that will be based on your assigned name or uploaded file name.
Array: $allowed_extensions
If you have set "extension_check" TRUE, in the $args array, than you can modify default list to allow or disallow in the following format.
/*
|---------------------------
| Upload Inputs
|---------------------------
*/
$args = array(
'upload_file' => $file,
'upload_directory' => $upload_directory,
'allowed_size' => 512000,
'extension_check' => TRUE,
'upload_overwrite' => FALSE,
);
$allowed_extensions = array(
'pdf' => FALSE,
);
Upload Hook
Upload Hook is very powerful feature of Chip PHP upload class. This hook will provide you a detail analysis of uploaded data after processing but before moving file to the directory, So you have a flexibility to do anything before processing the file to the upload directory. A sample code and output of this hook will be like,
/*
|---------------------------
| Upload Hook
|---------------------------
*/
$upload_hook = $object->get_upload( $args, $allowed_extensions );
Array
(
[upload_directory] => Valid - E:\wamp\www\tutorialchip\chip_upload/uploads/
[upload_directory_writable] => Valid - Directory is writable
[upload_file_extension] => Valid - Extension is allowed
[upload_file_size] => Valid - Size 97854 bytes
[upload_process] => 4
[upload_move] => 1
[upload_overwrite] =>
[upload_file] => Array
(
[name] => nature.jpg
[type] => image/jpeg
[tmp_name] => E:\wamp\tmp\phpAA65.tmp
<div class="error"><div class="box-content">Content</div><div class="clear"></div></div> => 0
[error_status] => All OK
[size] => 97854
[directory] => E:\wamp\www\passive\tutorialchip\library\chip_upload/uploads/
[nameonly] => nature
[extension] => jpg
)
)
Move File
You have take your time and decision by using the power of "$upload_hook" with class built-in method "get_upload". You can track any error by getting print of $upload_hook. Now it is time to move the file to the destination directory.
/*
|---------------------------
| Move File
|---------------------------
*/
if( $upload_hook['upload_move'] == TRUE ) {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
/*
|---------------------------
| Move File
|---------------------------
*/
$upload_output[] = $object->get_upload_move();
} else {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
}
Complete Tutorial – PHP File Upload Class / Script Example
/*
|-----------------
| Chip Constant Manipulation
|------------------
*/
define( "CHIP_DEMO_FSROOT", __DIR__ . "/" );
/*
|-----------------
| POST
|------------------
*/
if( $_POST ) {
/*
|-----------------
| Chip Upload Class
|------------------
*/
require_once("class.chip_upload.php");
/*
|-----------------
| Upload(s) Directory
|------------------
*/
$upload_directory = CHIP_DEMO_FSROOT . "uploads/";
/*
|-----------------
| Class Instance
|------------------
*/
$object = new chip_upload();
/*
|-----------------
| $_FILES Manipulation
|------------------
*/
$files = $object->get_upload_var( $_FILES['upload_file'] );
/*
|-----------------
| Upload File
|------------------
*/
foreach( $files as $file ) {
/*
|---------------------------
| Upload Inputs
|---------------------------
*/
$args = array(
'upload_file' => $file,
'upload_directory' => $upload_directory,
'allowed_size' => 512000,
'extension_check' => TRUE,
'upload_overwrite' => FALSE,
);
$allowed_extensions = array(
'pdf' => FALSE,
);
/*
|---------------------------
| Upload Hook
|---------------------------
*/
$upload_hook = $object->get_upload( $args, $allowed_extensions );
/*
|---------------------------
| Move File
|---------------------------
*/
if( $upload_hook['upload_move'] == TRUE ) {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
/*
|---------------------------
| Move File
|---------------------------
*/
$upload_output[] = $object->get_upload_move();
//$object->chip_print( $upload_output );
} else {
/*$temp['uploaded_status'] = FALSE;
$temp['uploaded_file'] = $upload_hook['upload_file']['name'] ;
$upload_output[] = $temp;*/
}
} // foreach( $files as $file )
} // if( $_POST )
It is the ideal class to quickly integrate file upload and manipulation in your site. This script will allow you to upload files from your browser to your hosting, using PHP.
Features
Uploads any image / file
File name extension validation to accept only files with a given list of extensions
Check whether the files exceeds the given size limit
Safely renames the file – removes spaces, special characters etc
Options to overwrite or make unique
File extension validation
Flexible error reporting system
Check if a directory and/or file exist
Chip PHP upload class allows you to select and upload multiple files at once rather than having to select and upload each file individually.
Requirements
PHP5 or higher
Learning and handling of PHP upload class by TutorialChip is very easy and flexible. I have also given a complete demo and usage in the download archive for your understanding, and i am going to explain the installation of this class step by step online as well.
Client Side Tutorial: File Upload HTML Form
It is the very important step to prepare the form for uploading files.
First important thing is to set "enctype" attribute in the form tag. i.e enctype="multipart/form-data"
PHP Upload Class is very powerful as it can handle "Single/Multiple" uploads easily, So you have to define the file input as an array i.e upload_file[]. You can view in the given code snippet.
File Upload Form: Single File Example
<form method="post" action="" enctype="multipart/form-data">
<p>Upload File: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<input type="submit" name="submit" value="Upload File" />
</form>
File Upload Form: Multiple File Example
<form method="post" action="" enctype="multipart/form-data">
<p>Upload File 1: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 2: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 3: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 4: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<p>Upload File 5: <input name="upload_file[]" id="upload_file[]" type="file" class="inputtext" /></p>
<input type="submit" name="submit" value="Upload File" />
</form>
Server Side Tutorial
As we have already prepared our form to send data to the PHP server to upload files. Now it is time to learn PHP upload class handling to complete this process smoothly.Auto Detection of Script Directory
PHP Upload Class script will try to detect the her directory path automatically by built-in Magic Constant of PHP i.e __DIR__. However you can alter it according to your need.
/*
|-----------------
| Chip Constant Manipulation
|------------------
*/
define( "CHIP_DEMO_FSROOT", __DIR__ . "/" );
Include "class.chip_upload.php"
You have to include PHP upload file in your file at the suitable position you think. I will recommend to include this class in your POST block. Don’t worry about POST block, you can easily understand it as the tutorial moves and from a descriptive demo in the download archive.
/*
|-----------------
| Chip Upload Class
|------------------
*/
require_once("class.chip_upload.php");
Upload Directory Path
Next step is to define upload directory path. This is the directory where you want to save your uploaded files. I have defined it "uploads", which is a directory in the download archive. Make sure that this directory should be writable.
/*
|-----------------
| Upload(s) Directory
|------------------
*/
$upload_directory = CHIP_DEMO_FSROOT . "uploads/";
Class Instance
It is time to make instance of PHP upload class now.
/*
|-----------------
| Class Instance
|------------------
*/
$object = new chip_upload();
$_FILES Manipulation
Our form will send files in the "upload_file" array which is packed in $_FILES. Our class is very powerful to handle Single and Multiple files efficiently, so we will pass this array to the "get_upload_var" method which will arrange this array for next processing professionally.
/*
|-----------------
| $_FILES Manipulation
|------------------
*/
$files = $object->get_upload_var( $_FILES['upload_file'] );
Powerful Upload Loop
Now we will start loop to upload files one by one. Let’s dissect this loop part by part. We are going to provide "$files" array to this loop.
/*
|-----------------
| Upload File
|------------------
*/
foreach( $files as $file ) {
Powerful Upload Loop
It is time to prepare inputs for class operation. Chip Upload class will take two parameters,
Array: $args
upload_file (Array): It will hold uploaded file array. You have to simply pass $file to this array as we are dissecting foreach loop on $file variable.
upload_directory (String – Correct Absolute Path): You have to provide a correct absolute path to this variable. We have already calculated this variable as $upload_directory
allowed_size (Integer – In Bytes): You have to provide maximum upload size to this part. You have to given size in bytes. 1Kb = 1024 bytes
extension_check (Boolean – TRUE | FALSE): This part will allow you to control over specific files to upload.
upload_overwrite (Boolean – TRUE | FALSE): This part will allow you to control for over-writing existing file or not. If set False, PHP upload class will automatically determine new name that will be based on your assigned name or uploaded file name.
Array: $allowed_extensions
If you have set "extension_check" TRUE, in the $args array, than you can modify default list to allow or disallow in the following format.
/*
|---------------------------
| Upload Inputs
|---------------------------
*/
$args = array(
'upload_file' => $file,
'upload_directory' => $upload_directory,
'allowed_size' => 512000,
'extension_check' => TRUE,
'upload_overwrite' => FALSE,
);
$allowed_extensions = array(
'pdf' => FALSE,
);
Upload Hook
Upload Hook is very powerful feature of Chip PHP upload class. This hook will provide you a detail analysis of uploaded data after processing but before moving file to the directory, So you have a flexibility to do anything before processing the file to the upload directory. A sample code and output of this hook will be like,
/*
|---------------------------
| Upload Hook
|---------------------------
*/
$upload_hook = $object->get_upload( $args, $allowed_extensions );
Array
(
[upload_directory] => Valid - E:\wamp\www\tutorialchip\chip_upload/uploads/
[upload_directory_writable] => Valid - Directory is writable
[upload_file_extension] => Valid - Extension is allowed
[upload_file_size] => Valid - Size 97854 bytes
[upload_process] => 4
[upload_move] => 1
[upload_overwrite] =>
[upload_file] => Array
(
[name] => nature.jpg
[type] => image/jpeg
[tmp_name] => E:\wamp\tmp\phpAA65.tmp
<div class="error"><div class="box-content">Content</div><div class="clear"></div></div> => 0
[error_status] => All OK
[size] => 97854
[directory] => E:\wamp\www\passive\tutorialchip\library\chip_upload/uploads/
[nameonly] => nature
[extension] => jpg
)
)
Move File
You have take your time and decision by using the power of "$upload_hook" with class built-in method "get_upload". You can track any error by getting print of $upload_hook. Now it is time to move the file to the destination directory.
/*
|---------------------------
| Move File
|---------------------------
*/
if( $upload_hook['upload_move'] == TRUE ) {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
/*
|---------------------------
| Move File
|---------------------------
*/
$upload_output[] = $object->get_upload_move();
} else {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
}
Complete Tutorial – PHP File Upload Class / Script Example
/*
|-----------------
| Chip Constant Manipulation
|------------------
*/
define( "CHIP_DEMO_FSROOT", __DIR__ . "/" );
/*
|-----------------
| POST
|------------------
*/
if( $_POST ) {
/*
|-----------------
| Chip Upload Class
|------------------
*/
require_once("class.chip_upload.php");
/*
|-----------------
| Upload(s) Directory
|------------------
*/
$upload_directory = CHIP_DEMO_FSROOT . "uploads/";
/*
|-----------------
| Class Instance
|------------------
*/
$object = new chip_upload();
/*
|-----------------
| $_FILES Manipulation
|------------------
*/
$files = $object->get_upload_var( $_FILES['upload_file'] );
/*
|-----------------
| Upload File
|------------------
*/
foreach( $files as $file ) {
/*
|---------------------------
| Upload Inputs
|---------------------------
*/
$args = array(
'upload_file' => $file,
'upload_directory' => $upload_directory,
'allowed_size' => 512000,
'extension_check' => TRUE,
'upload_overwrite' => FALSE,
);
$allowed_extensions = array(
'pdf' => FALSE,
);
/*
|---------------------------
| Upload Hook
|---------------------------
*/
$upload_hook = $object->get_upload( $args, $allowed_extensions );
/*
|---------------------------
| Move File
|---------------------------
*/
if( $upload_hook['upload_move'] == TRUE ) {
/*
|---------------------------
| Any Logic by User
|---------------------------
*/
/*
|---------------------------
| Move File
|---------------------------
*/
$upload_output[] = $object->get_upload_move();
//$object->chip_print( $upload_output );
} else {
/*$temp['uploaded_status'] = FALSE;
$temp['uploaded_file'] = $upload_hook['upload_file']['name'] ;
$upload_output[] = $temp;*/
}
} // foreach( $files as $file )
} // if( $_POST )
0 comments:
Post a Comment