1

I use fopen to access and wirte on a json file my controller code is like this

$this->data['date'] = $this->issue_model->get_date();
        $this->data['com'] = fopen("assets/global/plugins/fullcalendar/demos/json/events.json", "r") or die("Unable to open file!");
        $txt = json_encode($this->data['date']);
        fwrite($this->data['com'], $txt);

at the beginning i wrote the whole path but it give me an error

fopen(base_url().'assets/global/plugins/fullcalendar/demos/json/events.json', "r") or die("Unable to open file!");

the error look like this

A PHP Error was encountered

Severity: Warning

Message: fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known

Filename: controllers/welcome.php

Line Number: 45

the problem is my website work just fine in my localhost but when i uploaded it on my server it don't access the file and write on it

can anybody tell me what is wrong with my code?

  • If it works on you localhost, you probably have a path problem on your server. What's the value of `base_url()`? – Anthony Jan 27 '17 at 08:22
  • http://legal.sunrise-resorts.com/ – Hany Hesham Jan 27 '17 at 08:25
  • Does it append the `/` after base_url()? If not, `assets/` will touch your domain name. – Anthony Jan 27 '17 at 08:32
  • it look like this in the end http://legal.sunrise-resorts.com/assets/global/plugins/fullcalendar/demos/json/events.json – Hany Hesham Jan 27 '17 at 08:33
  • Ok so if path is right, it' probably about right as said by @Mayank Pandeyz – Anthony Jan 27 '17 at 08:34
  • Because you are using the domain name to open your file (base_url()), fopen() is using network to get the file. Try to use the absolute or relative path without your domain name. See this [answer](http://stackoverflow.com/questions/15242972/codeigniter-dynamically-getting-relative-absolute-path-outside-of-application) to use some predefined variables to basepath. – Anthony Jan 27 '17 at 08:44

1 Answers1

2

The issue is related to the directory permission on your server. First of all check the directory permission:

$ ls -ld directory

Here's what it does:

-d, --directory list directory entries instead of contents, and do not dereference symbolic links

// For Linux

Provide the write permission, if its not there.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • Indeed you have to have the write permission. On Linux you can have something like `rwxrwxrwx` (r for read, w for write, x for execution) where each char can be replaced by a `-` if the right is not allowed. You server user (www-data if you're using Apache on Debian/Ubuntu) should have the write (w) right. Otherwise, it couldn't write into the directory. – Anthony Jan 27 '17 at 08:30
  • on the json file i already give it write permission or you Suggests to give the assets directory write permission ? – Hany Hesham Jan 27 '17 at 08:30
  • 1
    @HanyHesham you need to have the write permission on json/ the directory to create the json file inside. – Anthony Jan 27 '17 at 08:32
  • Follow what @HanyHesham said in last comment – Mayank Pandeyz Jan 27 '17 at 08:33