reich/upload

A great class to upload your file or files to the server, could be integreted with laravel

v2.0 2017-09-10 17:26 UTC

This package is not auto-updated.

Last update: 2024-04-14 00:12:38 UTC


README

68747470733a2f2f64726976652e676f6f676c652e636f6d2f75633f6578706f72743d766965772669643d31386d3644797365696962746259433670614e556a4c5653337252425772735736

Upload.php


68747470733a2f2f7472617669732d63692e6f72672f6564656e72656963682f5048502d55706c6f61642d436c6173732e7376673f6272616e63683d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f72656963682f75706c6f61642f646f776e6c6f616473 68747470733a2f2f706f7365722e707567782e6f72672f72656963682f75706c6f61642f762f737461626c65 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f6564656e72656963682f5048502d55706c6f61642d436c6173732e737667 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6564656e72656963682f5048502d55706c6f61642d436c6173732e737667 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6564656e72656963682f5048502d55706c6f61642d436c6173732e737667 68747470733a2f2f706f7365722e707567782e6f72672f72656963682f75706c6f61642f6c6963656e7365

PHP Class for uploading file or files to the server

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