-1

HOW IS THIS DUPLICATE? The solution in "can-i-read-a-txt-file-with-php" does not work for me, because the code itself is fine, the error is because of the file path, I've tried different approaches, that's why I came here, to get some help. I tried the code that's on there anyway, still the exact problem; Warning: fopen(C:\dorian.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\filestats\index.php on line 9

Warning: filesize(): stat failed for C:\dorian.txt in C:\xampp\htdocs\filestats\index.php on line 10

Warning: fread() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\filestats\index.php on line 10

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\filestats\index.php on line 11

I'm trying out this simple code on PHP, I can't figure out what the trouble is. I'm trying to read what's in the file.

Here's the code.

<?php
$fpath = "C:\dorian.txt";
$handle = fopen($fpath, "rb");
$contents = fread($handle, filesize($fpath));
fclose($handle);
?>

I also tried with dorian.txt in a different directory, copy-pasted it from file properties.

1 Answers1

0

You need to escape backslashes, for Windows this will look like:

<?php
$fpath = "C:\\dorian.txt";
$handle = fopen($fpath, "rb");
$contents = fread($handle, filesize($fpath));
fclose($handle);
?>

I believe you can also use forward slashes instead.

Source: http://www.php.net/manual/en/function.fopen.php

mquinn
  • 454
  • 4
  • 14