PHP Classes

Simple PHP File Uploader: Validate and process uploaded files

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 566 All time: 5,407 This week: 488Up
Version License PHP version Categories
php-file-uploader 1.0GNU General Publi...5.3HTTP, PHP 5, Files and Folders
Description 

Author

This class can Validate and process uploaded files.

It takes an array entry of the $_FILES super-global variable for a given uploaded and file and the path of a directory to copy the uploaded file.

The class also provides a fluent interface to perform several types of validation like limiting the size of the uploaded file, or check if the file name extension or MIME type is one of the allowed, or check if a file already exists with same name in the destination directory.

A validated file is moved to the destination directory eventually renamed if there was already a file with the original name.

Picture of Pak Xpertz
  Performance   Level  
Name: Pak Xpertz <contact>
Classes: 2 packages by
Country: Pakistan Pakistan
Age: ???
All time rank: 241431 in Pakistan Pakistan
Week rank: 178 Up3 in Pakistan Pakistan Up

Recommendations

Upload any type of files
Check all the files based on the content

PHP code to identify uploaded file format
I need a code to identify the file format of uploaded file

Example

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Uploader Class Example</title>

        <!-- Bootstrap -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <div class="col-xs-8 col-xs-offset-2">
                    <?php
                   
require 'class.UploaderException.php';
                    require
'class.Uploader.php';
                    try {
                       
$file = new Uploader($_FILES["myfile"], "images/");
                       
$name = $file->setMaxSize(3)
                                ->
setAllowedExtensions(array(".doc",".docx"))
                                ->
renameIfExists()
                                ->
upload()
                                ->
getUploadedFileName();
                        echo
"<div class='alert alert-success'>File Uploaded ($name)</div>";
                    } catch (
UploaderException $e) {
                        echo
"<div class='alert alert-danger'>".$e->getMessage()."</div>";
                    }
                   
?>
<form method="post" action="index.php" enctype="multipart/form-data">
                        <div class="form-group">
                            <label>File:</label>
                            <input type="file" name="myfile" class="form-control" />
                        </div>
                        <input type="submit" name="upload" value="Upload" class="btn btn-primary" />
                    </form>
                </div>
            </div>
        </div>

        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    </body>
</html>


  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file index.php Example Example of using Uploader class
Plain text file class.UploaderException.php Class Exception Handler CLass
Plain text file class.Uploader.php Class Uploader Class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:566
This week:0
All time:5,407
This week:488Up