dongwang6837 2014-07-03 06:23
浏览 72
已采纳

fopen()没有使用php在同一目录中找到文件

I am attempting to use fopen() to allow me to pass values from csv cells to an Oracle 11g Database.

The csv was 'created' when a user uploads an xls via a html form. From there, I converted 'file.xls' to 'file.csv'. Up to this part, I have been successful. However, upon using fopen() [line 33, it is marked in notes], it fails to find the obviously created 'file.csv'. Is my notation wrong for using fopen()?

handler:

$allowed =  array('xls');
$filename = $_FILES['uploaded']['name'];
$file = $_FILES['uploaded']['tmp_name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(in_array($ext,$allowed) ) {

    $inputFileType = 'Excel5';
    $inputFileName = $file;

    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcelReader = $objReader->load($inputFileName);

    $loadedSheetNames = $objPHPExcelReader->getSheetNames();

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');

    foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
            $objWriter->setSheetIndex($sheetIndex);
            $objWriter->save('file'.'.csv');}

    }
    $fopen = fopen('file.csv', 'r'); //**line 33**//
    if($fopen) {
            while (($line = fgetcsv($files)) !== FALSE) {
            $csv_array[] = array_combine(range(1, count($line)), array_values($line));
            }
    }

It may be nice to note that I do plan on adding a delete section to delete 'file.csv'. I am only using it to update the database, then it is useless to me. So, if there is a more efficient way than saving it, updating db, deleting it, I would love to know :). Thanks, SO community!

展开全部

  • 写回答

1条回答 默认 最新

  • doushangan3690 2014-07-03 06:37
    关注

    It all depends on where $objWriter->save() will save the file. If that is the same folder as the input file then it will be your temporary directory (/tmp/ most likely on *nix) And it is not likely that open() will look there by default.

    You can avoid these problems my making all paths absolute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部