edenreich / PHP-Upload-Class by edenreich

A great class to upload your file or files to the server, could be integreted with laravel
131
10
3
Package Data
Maintainer Username: edenreich
Maintainer Contact: eden.reich@gmail.com (Eden Reich)
Package Create Date: 2016-02-01
Package Last Update: 2019-02-24
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-12 03:13:08
Package Statistics
Total Downloads: 131
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 10
Total Watchers: 3
Total Forks: 5
Total Open Issues: 1

Upload.php

Installing

with composer just run:

composer require reich/upload

or

You can also download src/Upload.php and simply use it.

As a Package for Laravel

This class is fully integrated into laravel framework using Laravel Auto-Discovery. If you don't use v5.5+ you may import the necessary files path in your /config/app.php file manually:

  ...
    "providers" => [
      /*
       * Package Service Providers...
       */
      Reich\Upload\Laravel\UploadServiceProvider::class,
    ],

    "aliases" => [
      "Upload" => Reich\Upload\Laravel\UploadFacade::class,
    ]
  ...

Usage

1) Copy the class that located in the src directory into your project or install it via composer and use it.
2) Generate an encryption key contains 32 characters or use Reich\Upload::generateMeAKey(); helper.
3) Please open the example /demo/index.php file I created to follow and get a better understanding

Make sure the form is submitted:

if(Upload::submitted())
{
  // rest of the code goes here
}

Make an instance of the class

$upload = new Upload(YOUR-HTML-INPUT-NAME); 

Set the directory where you want to upload the files, by default it will upload to your main directroy

$upload->setDirectory('img/'); 

You may also specify that you want to create this directory if it's not exists

$upload->setDirectory('img/')->create(true); 

You can set the rules you want for your upload using the following syntax:

$upload->addRules([
        'size' => 2000,
        'extensions' => 'png|jpg|pdf'
]);

or

$upload->addRules([
        'size' => 2000,
        'extensions' => ['png', 'jpg', 'pdf']
]);

Set this only if you want to have a encrypt file names(optional for security):

$upload->encryptFileNames(true);

You may also specify that you want only certain file type to be encrypted like so:

$upload->encryptFileNames(true)->only(['jpg']); // only jpg files will be encrypted

Or also the following syntax:

$upload->encryptFileNames(true)->only('jpg|png|txt'); // only jpg, png and txt files will be encrypted

After all is set just run the following command

$upload->start();

Events

Whenever a file has been successfully uploaded.

$upload->success(function($file) {
  // handle the file
});

If something went wrong listen to error.

$upload->error(function($file) {
  // handle the file
});

Error Handling

Check wether there are errors and if there arent errors, proccess the upload:

if($upload->unsuccessfulFilesHas())
{
  // display all errors with bootstraps
  $upload->displayErrors();

  // now of course you may formating it differently like so
  foreach($upload->errorFiles as $file)
  {
    // do whatever you want with the file object
    // - $file->name
    // - $file->encryptedName *only if you asked to encrypt*
    // - $file->type
    // - $file->extension
    // - $file->size
    // - $file->error
    // - $file->errorMessage
  }
}
else if($upload->successfulFilesHas())
{
  $upload->displaySuccess();

  // now of course you may formating it differently like so
  foreach($upload->successFiles as $file)
  {
    // do whatever you want with the file object
    // - $file->name
    // - $file->encryptedName *only if you asked to encrypt*
    // - $file->type
    // - $file->extension
    // - $file->size
  }
}

Here is another method to show you useful errors if something went wrong:

print_r($upload->debug()); // There are some errors only you should look at while setting this up