0

I have a submit file code that uploads an Excel file to my server and then uses it through the function.

When doing so:

    if ($this->upload->do_upload()){
        $data = array('upload_data' => $this->upload->data());

        $filename = $data['raw_name'];
        $fullfilename = $data['orig_name'];

        $inputFileName = FCPATH."files/automation/1/$filename.xlsx";
        $inputFileNameNew = FCPATH."files/automation/1/$filename-new.xlsx";

It gives me this output:

Error loading file ".xlsx": Could not open /var/www/html/tools/files/automation/1/.xlsx for reading! File does not exist.

Where I suspect it's because it hasn't found the file because it wasn't uploaded yet. Could be?

Imnotapotato
  • 5,308
  • 13
  • 80
  • 147

1 Answers1

1

if you want to upload a file in diferents folders by user or whatever you need to do this

public function upload_f(){
    $config['upload_path']      = FCPATH . '/files/automation/' . $id . '/';
    $config['allowed_types']    = 'xls|xlsx';

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

    if( ! $this->upload->do_upload()){
        $this->session->set_flashdata('upload-no', $this->upload->display_errors());
    }
    else{
        $this->_read_file(FCPATH . '/files/automation/' . $id . '/' . $this->upload->file_name);
    }
}

private funtion _read_file( $file ){
    $this->load->library('excel');
    $this->load->library('table');

    $file               = str_replace('//', '/', $file);

    $objPHPExcel = PHPExcel_IOFactory::load($file);

    $cell_collection    = $objPHPExcel->getActiveSheet()->getCellCollection();
    $lastRow            = $objPHPExcel->getActiveSheet()->getHighestRow();

    foreach ($cell_collection as $cell) {
        $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
        $row    = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
        $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();

        if ($row == 1) {
            $header[$row][$column] = $data_value;
        } 
        else{
            $arr_data[$row][$column] = $data_value;
        }
    }

    $this->table->set_heading('ID', 'value1', 'value2');

    for($i = 2; $i <= $lastRow; $i++){
        $_table= array($i, $arr_data[$i]['A'], $arr_data[$i]['B']);

        $this->table->add_row($_table);
    }

    echo $this->table->generate();
}
elddenmedio
  • 1,030
  • 1
  • 8
  • 15
  • No, I want to upload an Excel file and right after I upload it - I want to edit the Excel file. But when doing this on my current code it doesn't recognize the file as if it's not there, guessing that the code runs fast and doesn't wait for the whole file to get uploaded to run queries on it. – Imnotapotato Jun 02 '16 at 06:51
  • Yes, but the code that i uploaded is for upload and read, after you are sure that you can read the file, you can start edit the content. With the code you can read the file? – elddenmedio Jun 02 '16 at 16:09