0

I want to upload excel file into a folder and then upload the file name into database, but it gives me blank page with the url "http://localhost/myweb.com/admin/upload?file=tes+upload.xlsx". "tes upload" is the file name. The file didn't upload to the folder as well as to database. I don't know what I'm missing. I set the folder permission to 777.

My form :

<form action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
    <input type="file" name="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
    <input type="submit" class="btn btn-success" value="submit">
</form>

My controller :

function upload()
{
    if (!empty($_FILES))
    {
        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.'));
        $file_ext = substr($filename, strripos($filename, '.'));
        $tempFile = $_FILES['file']['tmp_name'];
        $data = "admin".uniqid().$file_ext;
        $targetPath = getcwd() . '/uploads/';
        $targetFile = $targetPath . $data ;

        move_uploaded_file($tempFile, $targetFile);

        $file_path =  FCPATH.'/uploads/';
        require_once APPPATH."/third_party/PHPExcel.php";
        require_once APPPATH . "/third_party/PHPExcel/IOFactory.php"; 

        $inputFileName = $file_path; 
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
        $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        $arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
        $regex = "~\d{5}~";
        $dataexcel = Array();

        for($i=1;$i<=count($dataexcel);$i++)
        {
            $dataexcel[$i]['to_name']=$allDataInSheet[$i]["A"];
            $dataexcel[$i]['to_address']=$allDataInSheet[$i]["B"];
            $dataexcel[$i]['to_phone']=$allDataInSheet[$i]["C"];
            preg_match($regex, $allDataInSheet[$i]["B"], $result);
            $dataexcel['to_zipcode'] = $result[0];
        };

        $data_user = array(
            'status'         => '1',
            'filename_admin' => $data,
        );

        $this->load->model('excel');
        $this->excel->upload_excel($allDataInSheet,$data_user);
    }
}

My model :

function upload_excel($allDataInSheet)
{
    $regex = "~\d{5}~";

    array_shift($allDataInSheet);

    foreach ($allDataInSheet as $key)     
    {
        preg_match($regex, $key['B'], $result);

        $data = array(
        'request_id'         =>   $request_id,
        'to_name'            =>   $key['A'],
        'to_phone'           =>   $key['C'],
        'to_address'         =>   $key['B'],
        'to_zipcode'         =>   $result[0],
        'tariff'             =>   '0'
        );

        $this->db->insert('excel', $data);
        $this->db->update('request',$request_id);
    }
}

Help me please. Thanks in advance

wpclevel
  • 2,451
  • 3
  • 14
  • 33
may
  • 675
  • 1
  • 10
  • 26
  • add this to the beginnig of your file and tell us what it says `ini_set('display_errors', 1); error_reporting(~0);` – Ognj3n Jun 07 '16 at 05:59
  • Put it in all 3 of your files, because you need to see your error logs in order to figure out what's wrong.. – Ognj3n Jun 07 '16 at 06:03
  • See this question and good luck with debugging http://stackoverflow.com/questions/3209807/how-to-do-error-logging-in-codeigniter-php – Ognj3n Jun 07 '16 at 06:14
  • I can't see that your controller is returning anything. Doesn't that make a blank page a correct result? – M. Eriksson Jun 07 '16 at 06:18
  • Also, I see you are using PHPExcel, have you included it into your files? Something like `require '../Classes/PHPExcel/IOFactory.php';` If you have, than see does your server has enabled php_zip extension – Ognj3n Jun 07 '16 at 06:18
  • i added load->view after that, but the file didn't upload either. It shows the page but no error :( @MagnusEriksson – may Jun 07 '16 at 06:20
  • yes, I added require_once @Ognj3n – may Jun 07 '16 at 06:21
  • the problem is the file didn't upload to the specify folder. do you find a mistake(s) in my controller? @Ognj3 – may Jun 07 '16 at 06:25
  • Good, but you still need to see does your server has `php_zip` and all other extensions enabled in order to work with PHPExcel. See here requirements https://phpexcel.codeplex.com/wikipage?title=Requirements&referringTitle=Home just put phpinfo() function into your code and see – Ognj3n Jun 07 '16 at 06:25
  • yes, it does @Ognj3n – may Jun 07 '16 at 06:28
  • Everything looks good at first glance and since I cannot parse PHP mentally anymore that's all help I could offer you. Hope someone with more reputation and experience will help you. Good luck – Ognj3n Jun 07 '16 at 06:30
  • Have you checked the server logs? – M. Eriksson Jun 07 '16 at 09:06

0 Answers0