PHP code example of unamatasanatarai / lara-simple-ajax-uploader

1. Go to this page and download the library: Download unamatasanatarai/lara-simple-ajax-uploader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unamatasanatarai / lara-simple-ajax-uploader example snippets


   
   namespace App\Http\Controllers;
   
   use Illuminate\Support\Str;
   use Unamatasanatarai\SimpleAjaxUploader\FileUpload;
   
   class AssetsController extends Controller
   {
   
       public function image()
       {
           $upload_dir = public_path('u/');
           $uploader = new FileUpload('uploadfile');
           // Handle the upload
           $result = $uploader->handleUpload($upload_dir);
           if ( ! $result ) {
               exit(json_encode([ 'success' => false, 'msg' => $uploader->getErrorMsg() ]));
           }
           if ( ! $uploader->isWebImage($uploader->getSavedFile()) ) {
               return response()->json([
                   'success' => false,
                   'error'   => __('Można wgrać tylko obrazki/zdjecia'),
               ]);
           }
           $newFilename = Str::slug($uploader->getFileNameWithoutExt()) . '_' . time() . '_' . uniqid() . '.' . $uploader->getExtension();
   
           copy($upload_dir . $uploader->getFileName(), $upload_dir . $newFilename);
   
           return response()->json([
               'success'     => true,
               'fileFullUrl' => asset('u/' . $newFilename),
               'fileUrl'     => $newFilename,
           ]);
       }