无法使用fopen(php)打开文件 [英] Cannot open file using fopen (php)

查看:81
本文介绍了无法使用fopen(php)打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开文件以用php脚本读取,但是遇到了麻烦.

I am trying to open a file for reading in php script but having trouble.

这是我的代码

$fileHandle = fopen("1234_main.csv", "r")or die("Unable to open");
if (!file_exists($fileHandle))
{
    echo "Cannot find file.";
}

该脚本与我尝试读取的文件位于同一目录中,并且没有其他读取/写入权限错误,因为我可以在同一目录中创建/读取其他文件.

The script is in the same directory as the file I am trying to read and there are no other read/write permission errors as I can create/read other files in the same directory.

运行脚本时,我仅收到找不到文件"错误消息.为什么显示此错误消息?当然,如果fopen()无法打开文件,或die语句"应该结束脚本吗?

When I run the script I just get the "Cannot find file" error message. Why is this error message being shown? Surely if fopen() can't open the file the "or die statement" should end the script?

此外,当文件确实存在并且与脚本位于同一位置时,为什么不能打开该文件(我也尝试使用文件名的完整路径,而不仅仅是文件名).

Also, why can't I open the file when it definitely exists and is in the same location as the script (I have also tried using the full path of the filename instead of just the filename).

我对php还是很陌生(但是在c ++中有exp),所以如果这是一个愚蠢的问题,我表示歉意.

I am fairly new to php (but have exp in c++) so if its a stupid question I apologize.

非常感谢

推荐答案

file_exists()将文件名作为输入,但是代码的逻辑存在问题.您首先尝试打开一个文件,然后检查它的存在?

file_exists() take the file-name as input, but the logic of your code has problem. You first try to open a file then you check its existence?

您首先应通过 file_exists("1234_main.csv")检查其是否存在,如果存在,请尝试将其打开.

You first should check its existence by file_exists("1234_main.csv") and if it exists try to open it.

这篇关于无法使用fopen(php)打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆