“File Upload CodeIgniter” Kode Jawaban

Dapatkan Upload Kesalahan Codeigniter

if($this->input->post())
{
   $file_element_name = 'image';  //this is the name of your input file. for example "image"
   if ($_FILES['image']['name']!= "")
   {
      $config['upload_path'] = './uploads/';
      $config['allowed_types'] = 'gif|jpg|png|exe|xls|doc|docx|xlsx|rar|zip';
      $config['max_size']      = '8192'; 
      $config['remove_spaces']=TRUE;  //it will remove all spaces
      $config['encrypt_name']=TRUE;   //it wil encrypte the original file name
      $this->load->library('upload', $config);

      if (!$this->upload->do_upload($file_element_name))
      {
         $error = array('error' => $this->upload->display_errors());
         $this->session->set_flashdata('error',$error['error']);
         redirect('controller_name/function_name','refresh');
      }
      else
      {
         $data = $this->upload->data();
         return $data['file_name'];          
      }
      $this->session->set_flashdata('msg','success message');
      redirect('controller_name/function_name','refresh');
   }
   else
   {
        //if no file uploaded the do stuff here
   } 
}
Difficult Dormouse

File Upload CodeIgniter

<?php namespace App\Controllers;

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('/'); 
   }

}
Clumsy Corncrake

Jawaban yang mirip dengan “File Upload CodeIgniter”

Pertanyaan yang mirip dengan “File Upload CodeIgniter”

Lebih banyak jawaban terkait untuk “File Upload CodeIgniter” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya