1

I have to insert data of the excel file in MySql table which can be selected from a location and I am using Symfony, PHPExcel and Ajax for this. I have created a file upload button in table. I click on choose files, select the file and click on Upload File then I get error on the console like Could not open "C:\fakepath\CRS_Data.xls for reading! File does not exist". I have read so many same Questions here like this but I was not able to solve the problem even after reading these. Below is my code. So please someone suggest the solution of the problem.

<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#uploadFile').click(function(e){
var file_path = document.getElementById("file").value;
    e.preventDefault();
    var urlAjax = "<?php echo URL_AJAX . '/birthPublicSearch' ?>";

   // birthPublicSearch is the module name and UploadFile is action name

        $.ajax({
            url: urlAjax + "/UploadFile",
            data:   { 
                        file_path: file_path
                    },
            async: false,
            type: "POST",
            success: function(response){
                console.log(response);
               alert("Data Uploaded Successful");
            }
        });
    });  
}); 
</script>


<table border="0" cellspacing="2" cellpadding="2">            
   <tr>
     <td bgcolor="#E6E6FA"><font color="red">Upload File</font></td>
        <td></td>
        <td bgcolor="#E6E6FA">
            <input type="file" name="file" id="file">
        </td>
   </tr>
  <tr>
    <td colspan="2" align="center" style="padding-bottom: 10px;">
      <input type="button" id="uploadFile" name="uploadFile" value="Upload File" class="btn"></input>
    </td>
  </tr>
</table>

And the Action is defined as:

public function executeUploadFile(sfWebRequest $request) {

    require_once('/../Classes/PHPExcel.php');
    require_once '/../Classes/PHPExcel/IOFactory.php';

    $path=$_REQUEST['file_path'];

    // path I get is as C:\fakepath\CRS_Data.xls

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

    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet){
        $worksheetTitle = $worksheet->getTitle();
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    }

    //Inserting datas into db
    $i=1;
    $j=0;
    for ($row = 1; $row <= $highestRow; ++ $row) 
    {
        $val=array();
        for ($col = 0; $col < $highestColumnIndex; ++ $col) 
        {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val[] = $cell->getValue();
        }

        // Insert data into mysql
        $sql="INSERT INTO tempbackuptable(`bir_le_id`,`bir_le_form_no`,)VALUES($val[0], $val[1])";
        $result=mysql_query($sql);

        $i=$i+1;
    }

    $this->setLayout(false);
    return sfView::NONE;
}
Mukesh Joshi
  • 2,784
  • 4
  • 24
  • 34
  • Does your file really exists? – VeeeneX May 13 '15 at 13:18
  • 2
    Surely `$_REQUEST['file_path']` is set to the file path on the client, not on the server – Mark Baker May 13 '15 at 13:19
  • @VeeeneX: Yes, the file is inside a folder in my desktop. I am selecting that file and then click on Upload File. – Mukesh Joshi May 13 '15 at 13:21
  • @MarkBaker: Yes I am selecting the file from the folder in my Desktop. Can I not upload the data like this. What I have to do then. – Mukesh Joshi May 13 '15 at 13:23
  • @Mukesh Have you validated path? – VeeeneX May 13 '15 at 13:31
  • No! The file has to be uploaded to the webserver..... your PHP script is executing on the webserver.... your PHP script doesn't even know that you have a drive C:\ or a desktop folder – Mark Baker May 13 '15 at 14:10
  • Your code does not even bear a similarity to one that would handle file uploads. Please, read the documentation first: http://php.net/manual/en/features.file-upload.post-method.php – Marek May 13 '15 at 15:31
  • @MarkBaker : Thanks for your replies. It helped me and solved my problem to some extent but still having problem in inserting the data to database. The loop is not working correctly and not getting values according to indexes. – Mukesh Joshi May 14 '15 at 07:12

0 Answers0