0

I have mypage.php where I try to fopen a file, but I get:

Warning: fopen(/json/subcat/list.json): failed to open stream: No such file or directory

This is how I try to open file: $json = fopen("/json/subcat/list.json", "r"); This is the tree of project:

P.S.: I use xampp and root is .../htdocs/projectName/ and list.json contains {"list": ["a", "b"]}, if matter.

barell
  • 1,470
  • 13
  • 19
KunLun
  • 3,109
  • 3
  • 18
  • 65

3 Answers3

1

You are providing a full-path:

$json = fopen("/json/subcat/list.json", "r");

and you should be providing a relative path:

$json = fopen("json/subcat/list.json", "r");

Your full path is something like /var/www/htdocs/projectName/json/subcat/list.json.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
1

You could use a full path using $_SERVER['DOCUMENT_ROOT']

$json = fopen($_SERVER['DOCUMENT_ROOT'] . "/json/subcat/list.json", "r");

This will give you the file path to your document root, and then will append the rest of the path onto it.

Or you relative paths, which is:

$json = fopen("json/subcat/list.json", "r");

Without the first /

Check out this link for more information about relative and absolute paths: Absolute vs. relative paths

Adam
  • 1,294
  • 11
  • 24
  • First time I tried `json/subcat/list.json` but got that error, after that I put `/json/subcat/list.json` hoping it will work. – KunLun Feb 14 '19 at 16:49
  • That don't give me the right path. `C:/xampp/htdocs/json/subcat/list.json` is missing the name of project after `htdocs`. It should be `C:/xampp/htdocs/projname/json/subcat/list.json` – KunLun Feb 14 '19 at 16:53
  • Add the project name in there then? – Adam Feb 14 '19 at 16:53
  • `$json = fopen($_SERVER['DOCUMENT_ROOT'] . "/projectName/json/subcat/list.json", "r");` – Adam Feb 14 '19 at 16:54
  • `$_SERVER['DOCUMENT_ROOT']` gives you the path to your webroot... so that additional folder is your problem... `$json = fopen("/projectName/json/subcat/list.json", "r");` will probably work – Adam Feb 14 '19 at 16:55
  • So your questions image seems a bit wrong... you're root isn't the projectname... it's C:/xampp/htdocs – Adam Feb 14 '19 at 16:56
  • I added it, but still error. I copy pasted `C:/xampp/htdocs/projname/json/subcat` into explorer and it works. I don't get why php get me error. – KunLun Feb 14 '19 at 16:56
  • Can you add a screenshot of C:/xampp/htdocs in explorer so I can see filepaths? Perhaps another folder is required – Adam Feb 14 '19 at 16:56
  • If still not working, though path is now correct, then could be permissions. You may need to go to htdocs, and propagate the permissions down to child directories – Adam Feb 14 '19 at 16:57
  • Also, I presume you can actually access pages via the Broswers that are in htdocs? Can you add a new php file, called phpinfo.php... in there add and then access that page via your broswers? – Adam Feb 14 '19 at 16:59
  • Just try to dump the ($_SERVER['DOCUMENT_ROOT']) may be the result will help you – Tagarikdi Djakouba Feb 14 '19 at 17:02
  • There was a space into the name of file `list .json`. – KunLun Feb 14 '19 at 17:07
0

Try this:

//your project folder:
$projectFolde = "my_project_folder";

$json = fopen($_SERVER['DOCUMENT_ROOT']."/json/subcat/list.json", "r");