dongwang6837 2014-07-03 14: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 14: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.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失