无法使用fopen从PHP打开文件

时间:2016-05-06 06:35:42

标签: php file fopen

我正在尝试从php中的文本文件中读取内容。我在窗户上使用wamp。我收到这个错误:

警告:fopen(/input.txt):无法打开流:第3行的C:\ wamp \ www \ cycle_gps_sender.php中没有此类文件或目录

这是我的代码:

$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);

php文件和input.txt都放在wamp的www文件夹中。

7 个答案:

答案 0 :(得分:2)

希望这会对你有所帮助:

$File = "log_post.txt"; 
$fh = fopen ($File, 't') or die("can't open file");  
fclose($fh);

答案 1 :(得分:1)

$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);

使用此代码并将input.txt文件保存在编写此代码的同一目录中。

答案 2 :(得分:0)

首先检查文件exist是否成立?

 $filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    chmod($filename, 0777);
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

答案 3 :(得分:0)

$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;

$location = file_get_contents('input.txt');
echo $location;
希望它会有所帮助

答案 4 :(得分:0)

添加文件的完整路径。 在Windows平台上,请小心转义文件路径中使用的任何反斜杠,或使用正斜杠。

$location = fopen("C:\\folder\\input.txt", "r");

答案 5 :(得分:0)

删除 '/'(斜线)

$location = fopen("input.txt", "r") or die("Unable to open file!");

答案 6 :(得分:0)

$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);

您必须在READ之前创建文件或者按'r'打开文件,因此如果您通过'a +'打开文件,您的文件将自动创建。