0

In my code I create a file called $cookie. I than later want to open it and read contents from it, but I get the error fopen(): failed to open stream: No such file or directory. I have a feeling I messed up something related to paths. Any advice on how to tackle this?

I tried this:

<?php
$cookie = 'cookies3.txt';
..
$myfile = fopen("cookies3.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("cookies3.txt"));
fclose($myfile);
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
potato
  • 4,479
  • 7
  • 42
  • 99
  • `dirname($cookie)` do you really have a directory called `cookies3.txt`? http://php.net/manual/en/function.dirname.php *"Returns the path of a parent directory."* - Plus, make sure that cookies were first set before attempting to write them. You may have wanted to use `file()` http://php.net/manual/en/function.file.php not `dirname()`. – Funk Forty Niner Feb 23 '16 at 15:04
  • yeah that second part is something i found on so and gave it a try. i will delete it. – potato Feb 23 '16 at 15:10
  • check for folder and file permissions then. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Feb 23 '16 at 15:12
  • Your code works,given what you posted. It's most likely a permissions issue here. If that text file isn't in the same folder as your script, then you need to specify its correct path. Plus make sure the file does exist in the first place. – Funk Forty Niner Feb 23 '16 at 15:14
  • `file_get_contents();` don't put yourself in this trouble if it can be that easy, or wait: `file()` even returns an array for each \n isn't that amazingly easy? – Sainan Feb 23 '16 at 15:15

1 Answers1

2

Yep, the file wasn't created. All thanks to fred -ii-.

potato
  • 4,479
  • 7
  • 42
  • 99