• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optional file upload

#1
first question:

Why the file upload in COdeigniter is not optional so that I need to make a trick?

Second one:

In this code below If I select a file it says "You did not select a file to upload." and doesn't upload.
I have title,text,date and file fields in the form.

If I don't select a file there is no problem. Can you help me solve that?


PHP Code:
public function create()
    {
        
$this->load->helper('form');
        
$this->load->library('form_validation');

        
$this->form_validation->set_rules('title''Başlık''trim|required');
        
$this->form_validation->set_rules('text''Yazı''trim|required');
        
        
$page="create";
        
        if (
$this->form_validation->run() === FALSE)
        {
            
$this->show($page);
        }
        else
        {
            
$title=$this->input->post('title');
            
$text=$this->input->post('text');
            
$date=$this->input->post('date');
            
$f=$this->input->post('file');
            
//$this->load->library("uploader");
            
if ($_FILES['dosya']['name'] != ""){
                
$this->uploadfile();
            }else{
                
$message="added without file successfully";
                
$data['title']="Makale Listesi";
                
$data['list']=$this->makale_model->listing(0);
                
$data['page']=$page;
                
$data['message']=$message;
                
$this->show("modify",$data);
            }
            
            
            
//$this->makale_model->insert($title,$text,$f,$date);
            //$f=$data['']
        
}
    }

private function 
uploadfile()
    {
        
$config['upload_path'] = base_url().'/files/makaleler/';
        
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|pdf|doc|docx|ppt|pptx';
        
//$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->show("create",$error);
        }
        else
        {
            
$data = array('upload' => $this->upload->data());
            
$message="added successfully with file";
            
            
/*$data['title']="Listing";
            $data['list']=$this->makale_model->listing(0);
            $data['page']=$page;
            $data['message']=$message;
            $this->show("list",$data);
            */
            
$message=urlencode($message);
            
//redirect("makale/index?message=".$message,"refresh");
            
$this->show("modify",$data);
        }
    } 
Reply

#2
This is a shot in the dark, but did you use form_open_multipart() in your view, or just form_open()?
Reply

#3
PHP Code:
<?php if (isset($error))    echo $error;?>

<?php echo form_open_multipart('makale/create');?>

title<input type="text" name="title" /><br /> 
date <input type="text" name="date"  size="20" maxlength="20" /><br />
Text<textarea name="text"> </textarea><br /><br />
File <input type="file" name="dosya" /><br />

<input type="submit" value="send" />

<?php 
if(isset($uploaded_file)){
print_r($uploaded_file);
print 
$status_message;
print 
$status;
}

if(isset(
$error)){
    print 
$error;
}

?>
</form> 

Yes I had.
Reply

#4
Try changing $this->upload->do_upload() to $this->upload->do_upload('dosya')
Reply

#5
thank you for help. solved now.

I changed the upload path to
$config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"]).'/files/makaleler/';
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.