Check if the file will be downloaded? CodeIgniter

I have a form with several inputs and input files. I want to check if the input of the file is empty or not. If it is empty, do not try to download it if it is not trying to download it.

I tried something like this:

$upld_file = $this->upload->data(); if(!empty($upld_file)) { //Upload file } 
+4
source share
2 answers

you use the codeigniter file loader class ... and call $this->upload->do_upload(); in the ahd conditional expression, check if it is true.

 <?php if ( ! $this->upload->do_upload()){ $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else{ $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } 

User_guide explains this in detail: http://codeigniter.com/user_guide/libraries/file_uploading.html

However, if you are dead, set to check if the file was โ€œuploadedโ€ aka .. presented before you call this class (you donโ€™t know why you did it). You can access PHPs $_FILES super global .. and use a conditional expression to check for size> 0.

http://www.php.net/manual/en/reserved.variables.files.php

Update 2: This is the actual working code, I myself use it on the avatar loader using CI 2.1

 <?php //Just in case you decide to use multiple file uploads for some reason.. //if not, take the code within the foreach statement foreach($_FILES as $files => $filesValue){ if (!empty($filesValue['name'])){ if ( ! $this->upload->do_upload()){ $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); }else{ $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } }//nothing chosen, dont run. }//end foreach 
+6
source

You may need more information. But basically, using the codeigniter loading class does something like this:

 $result = $this->upload->do_upload(); if($result === FALSE) { // handle error $message = $this->upload->display_errors(); } else { // continue } 

There are many features in codeigniter, you may not need to reinvent the wheel here.

0
source

Source: https://habr.com/ru/post/1396397/


All Articles