0

i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:

Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3

this is my code:

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

both the php file and input.txt are placed in www folder of wamp.

user3942918
  • 25,539
  • 11
  • 55
  • 67
Vikas Zingade
  • 134
  • 1
  • 2
  • 5
  • try to use full path of your file. if it still cannot be opened, it should be the permission problem. – Jacky Shek May 06 '16 at 06:38
  • Also, if the .txt file is in the same directory of your PHP file, remove the `/` or just do parent directories. I've had similar problems and it was cause it didn't know what the `/` was. Hope this helps. – Jack Hales May 06 '16 at 06:40
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew May 06 '16 at 19:50

7 Answers7

2

Hope this will help you:

$File = "log_post.txt"; 
$fh = fopen ($File, 't') or die("can't open file");  
fclose($fh);
Colin Cline
  • 1,291
  • 1
  • 12
  • 25
1
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);

Use this code and keep the input.txt file in the same directory where this code is written.

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • There is a troubleshooting checklist for this error : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:51
0

First check if file exist or not?

 $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";
}
user2134
  • 177
  • 2
  • 15
  • There is a troubleshooting checklist for this error : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:52
0
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;

or

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

hope it will help

Naisa purushotham
  • 905
  • 10
  • 18
  • There is a troubleshooting checklist for this error : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:52
0

Add full path to file. On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.

$location = fopen("C:\\folder\\input.txt", "r");
ssnake
  • 365
  • 2
  • 14
  • There is a troubleshooting checklist for this error : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:52
0

Remove '/'(Slash)

$location = fopen("input.txt", "r") or die("Unable to open file!");
KAREN
  • 388
  • 2
  • 10
  • There is a troubleshooting checklist for this error : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:52
0
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);

You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.