Home > Net >  Codeigniter do_upload method is not working
Codeigniter do_upload method is not working

Time:08-09

I am working on a cms with Codeigniter. I am trying to upload file from system. I am receiving the file but the do_upload method is not working. There are similar questions on this topic, but none of the answers of them worked.

Here is my method:

public function fileupdate($productId=False){
    
    $formData = $this->input->post();

    if (isset($formData['activityNewFile'])) {
        
        foreach ($formData['activityNewFile'] as $key => $value) {
            $rndNumber = rand(1,500);
            $fileLastName = sef_url($formData['activitynewfilename'.$value]);
            // Set preference 
            $config['upload_path']   = '../assets/'; 
            $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx'; 
            $config['max_size']      = '80960'; // max_size in kb 
            $config['file_name']     = $fileLastName; 
            $upFile                  = 'activitynewfile'.$value;
         
          
            // Load upload library 
            $this->load->library('upload',$config); 
            // File upload
            if($this->upload->do_upload($upFile)){ 
                 // Get data about the file
                 $uploadData = $this->upload->data(); 
                 $filename = $uploadData['file_name']; 

                $daData = array(
                    'productId'        => $productId,
                    'productFileName'  => $formData['activitynewfilename'.$value],
                    'productFile'      => $filename
                );
                $productfileInsert = $this->db->insert('productfile',$daData);
            
            }
            
        }

    }

    $this->success("İşlem tamamlandı.");

}

HTML part:

<form  role="form" method="POST" id="product_file_edit" action="<?=base_url('product/product/fileupdate/'.$productInfo->productId);?>" enctype="multipart/form-data"  onsubmit="return false; form_gonder('product_file_edit')" >
            <div >
    
                <div >
                    <div >
                        <div  style="margin-bottom: 10px;padding: 0;" id="file_row">
                            <button  onclick="addFileRow('file_add')" >
                                <i ></i> Add
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                <button  onclick="form_gonder('product_file_edit')"  >
                    Save
                </button>
            </div>
        </form>

Javascript part:

function addFileRow(tur){

        if(tur=="file_add"){

          emreFile = Math.floor(Math.random() * 10);

          myhtmlFile = '<div  id="filenew' emreFile '" style="padding: 0;margin: 0;margin-bottom: 10px;"><div ><input type="hidden" name="activityNewFile[]" value="' emreFile '"><img src="../assets/file/pdfword.png" style="width: 38px;float: left;height: 38px;"><input type="text"  name="activitynewfilename' emreFile '"   placeholder="Dosya Adı" style="width: 45%;float: left;height: 39px;"><input type="file"  name="activitynewfile' emreFile '"  accept=".xls,.xlsx,.doc,.docx,.pdf"   placeholder="Dosya Adı" style="width: 46%;opacity: 1;float: right;position: relative;"><span ><button  onclick="deleteNewFileRow(' emreFile ')" type="button"><i ></i></button></span></div></div>';

          var icerikFile = myhtmlFile;

          jQuery("#file_row").prepend(icerikFile);

        }
    }

I would be very grateful if you could find out what the problem is.

CodePudding user response:

Your problem is actually the file is not loading properly. There is no error in your codes. Make sure file_uploads is turned on on your server. Make sure the upload_max_filesize value is not smaller than your file size.

  • Related