Welcome Guest, Not a member yet? Register   Sign In
How to check the photo is selected when try to upload a photo
#1

[eluser]Zulkifli Said[/eluser]
hi,
i make a form to upload a text and a photo(* where optional to upload photo).

this my code in view:
Code:
<input type="text" name="firstname" />
<input type="file" name="userfile" id="file"/>

THE QUESTION IS:
how i can check the user upload/browse/select a photo/file or not??

my code like on controller like this:

Code:
$data['name'] = $this->input->post('firstname',TRUE);
$data['photo'] = $this->input->post('userfile',TRUE);
if(!isset($data['photo'])){
echo "you not select a photo/file" ;
}else{
echo "you select a photo/file";
}

But, i found the error...
if i not select a photo , the output is "you select a photo/file";

and if i select a photo , the output is "you select a photo/file";

i think the error is
1. $data['photo'] = $this->input->post('userfile',TRUE);
2. if(!isset($data['photo']))


please give me a solution.. thanks,,,
#2

[eluser]tastebuds[/eluser]

try using display_errors() function from the upload library
here is the snippet of code from user guide http://ellislab.com/codeigniter/user-gui...ading.html

Code:
<?php

class Upload extends CI_Controller {

function __construct()
{
  parent::__construct();
  $this->load->helper(array('form', 'url'));
}

function index()
{
  $this->load->view('upload_form', array('error' => ' ' ));
}

function do_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->library('upload', $config);

  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);
  }
}
}
?>
#3

[eluser]boltsabre[/eluser]
Your making a variable, $data['photo'], and assigning it the value of the post. Even if the post input (image) is empty, your variable $data['photo'] still exists!!!

Change to

[code]
if(!isset($data['photo']) || $data['photo'] == false){
...
}else{
...
}
#4

[eluser]Zulkifli Said[/eluser]
thanks for reply...
my problem is solving

i try using

Code:
if($_FILES['userfile']['error'] === 0 ){}

or

Code:
if($_FILES['userfile']['name']){}
#5

[eluser]boltsabre[/eluser]
no worries, at least you now know that you cant make a variable and then check isset() against it!!!

However, that may change in CI3, check this out: http://ellislab.com/forums/viewthread/215833/

But yeah, currently post variables return false if not set, when more than likely they should return null.

Try this just for your own knowledge:
Code:
$var = false;
if(isset($var)){
   echo "here";
}else{
   echo "not here";
}
//and now change $var = null; and see the difference.




Theme © iAndrew 2016 - Forum software by © MyBB