Search
 
SCRIPT & CODE EXAMPLE
 

PHP

File upload codeigniter

<?php namespace AppControllers;

class UsersController extends BaseController
{

   public function index(){
     return view('users');
   }

   public function fileUpload(){

     // Validation
     $input = $this->validate([
        'file' => 'uploaded[file]|max_size[file,1024]|ext_in[file,jpg,jpeg,docx,pdf],'
     ]);

     if (!$input) { // Not valid
         $data['validation'] = $this->validator; 
         return view('users',$data); 
     }else{ // Valid

         if($file = $this->request->getFile('file')) {
            if ($file->isValid() && ! $file->hasMoved()) {
               // Get file name and extension
               $name = $file->getName();
               $ext = $file->getClientExtension();

               // Get random file name
               $newName = $file->getRandomName(); 

               // Store file in public/uploads/ folder
               $file->move('../public/uploads', $newName);

               // File path to display preview
               $filepath = base_url()."/uploads/".$newName;
               
               // Set Session
               session()->setFlashdata('message', 'Uploaded Successfully!');
               session()->setFlashdata('alert-class', 'alert-success');
               session()->setFlashdata('filepath', $filepath);
               session()->setFlashdata('extension', $ext);

            }else{
               // Set Session
               session()->setFlashdata('message', 'File not uploaded.');
               session()->setFlashdata('alert-class', 'alert-danger');

            }
         }

     }
  
     return redirect()->route('/'); 
   }

}
Comment

PREVIOUS NEXT
Code Example
Php :: pretty json php 
Php :: Remove last symbol from string 
Php :: return two variables php 
Php :: ci base url dynamic 
Php :: Clear and delete the folder after the time specified in php 
Php :: Proc file for laravel 
Php :: laravel vue 
Php :: in_array validation laravel 
Php :: Unable to locate package php7.4 for kali 2021 
Php :: where () laravel Eloquent 
Php :: if exists in string php 
Php :: load-styles.php 403 
Php :: redirect stderr from echo 
Php :: php redirect with query string 
Php :: wp reserved image size names 
Php :: find days with name between two dates in php 
Php :: TRANSACTON LARAVEL QUERY BUILDER 
Php :: include in php 
Php :: how to get woocommerce order details 
Php :: wordpress is_tag function 
Php :: check if checkbox is not checked laravel 8 
Php :: doctrine querybuilder print sql 
Php :: function in php 
Php :: update column value laravel 
Php :: install php pdo mysql PHP5.6 alpine-apache 
Php :: route laravel Target class [AuthController] does not exist 
Php :: gettype() function in PHP 
Php :: Root composer.json requires php ^7.1.3 but your php version (8.0.3) does not satisfy that requirement. 
Php :: carbon compare same date 
Php :: laravel valet refresh env 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =