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 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004