doushi3819244 2013-09-07 03:41
浏览 112
已采纳

如何在PHP中检查正确的文件扩展名?

So basically, I am simply just trying to check for the correct file extension on a file that is being uploaded.

I know, this question has been answered on here a few times before, although I keep getting the same error and there is no solution or suggestions out there to why this is happening.

Here is my code:

$file = fopen($_FILES['upload_csv']['tmp_name'], 'r');
$ext = pathinfo($file, PATHINFO_EXTENSION);

if($ext != "csv")
{
    $errors[] = "Sorry, but only CSV files are supported";
}

Here is my error:

Warning: pathinfo() expects parameter 1 to be string

I have tried around 3 other alternatives now, all using pathinfo(). Although, the exact same error is still shown.

Does anyone have any suggestions to why this is happening?

  • 写回答

4条回答 默认 最新

  • dongyun6229 2013-09-07 03:47
    关注

    Your problem is here:

    $file = fopen($_FILES['upload_csv']['tmp_name'], 'r');
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    

    fopen returns a file handle for use reading and writing a file, but pathinfo is expecting a string containing a filename (optionally, with a path), but you're giving it a file handle.

    You should, in any case, be looking at $_FILES['upload_csv']['name'], which is the original name of the file, and extracting the file extension from that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM