Error creating a fopen file

Asked

Viewed 524 times

2

When trying to create a file in JSON format with fopen I am returned a permission error with the following message:

Warning: fopen(/json/9.json): failed to open stream: No such file or directory in C: xampp htdocs apil Controllers Classex.php on line 120

As I use Windows OS I have also already given permission in the folder I have already left pre-created, however the problem still persists.

Permission I gave everyone about the JSON file by right-clicking on the file->Properties->Share:

Permissão que dei a todos sobre o arquivo 'json' pressionando botão direito em cima do arquivo->Propriedades->Compartilhar

The code I’m using to create the file and then write the data to it follows:

 protected function filejson($product_id, $account_id) {
    $file = array($account_id);

    $json = fopen("/json/" . $product_id . ".json", "w");
    fwrite($json, json_encode($file));
    fclose($json);
}

And make the call of the method in another method that is in the same class as follows:

 Classex::filejson($product_id, $this->getAccount_id());

It is worth mentioning that both vestments $product_id and $account_id have the correct content or are not null.

I am using XAMPP in Windows 10.

1 answer

3


First, and important:

Sharing is not folder permission. Sharing is permission to access another PC folder (and modify its source including).

Folder permission is by clicking Properties > Security

According to:

There’s an extra bar at the beginning of the road, fopen is in relation to the file system, and not the site root:

$json = fopen("/json/" . $product_id . ".json", "w");
//             ^

If you want path relative to the executed file, you can use it like this:

$json = fopen("json/" . $product_id . ".json", "w");

Or so:

$json = fopen("./json/" . $product_id . ".json", "w");

Even so, you need to make sure that you are running the script in the correct folder, and that nothing previous in this code has changed the working directory.

An alternative would be to use some environment variable to produce the full path:

$root = $_SERVER['DOCUMENT_ROOT']; // Equivale ao "/" do site, dependendo 
                                   // do jeito que foi configurado o servidor
                                   // No seu caso vai retornar algo como: C:/xampp/htdocs

$json = fopen("$root/json/" . $product_id . ".json", "w");
  • Thanks in advance for the attention Bacco, But the same error still persists even implementing $_SERVER , will be that the error may be a problem in XAMPP?

  • Just to complement my last comment , when using $_SERVER basically it returns me the same error with the exact path of the file "fopen(C:/xampp/htdocs/json/14.json): failed to open stream: No such file or directory in ..."

  • Probably error on the way, try the full path for test purposes in format $json = fopen('C:/xampp/htdocs/json/9.json') for a fixed and existing test JSON. Take advantage of and view the error log if you have no base restriction or similar

  • Is it sure the path is the same? Have you checked the folder permissions for the page server user? json does not need to exist, which PHP creates, but the folder path does not.

  • Note that SHARE is not folder permission. Folder permission is in the security part.

  • I checked again and actually the file path was looking for the json folder in the htdocs folder and the correct one was the path $root/apil/Controllers/json/.&#Thank you very much for your attention Bacco!

  • Good that solved, just note the issue of sharing, undo it to not have undue access to the source file over the network

  • blz , I’ll be back to the default setting.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.