Why is the file I'm trying to write "nonexistent"?

I have my own configuration file in my CI application. In my web application admin panel, I want to have a form that I can use to change the values ​​of the config array of a user file. My problem right now is that the write_file function always returns false, and I don't know why. I am sure that I write the path in the function correctly, and the config directory is not read-only (this is wamp, therefore Windows). The file definitely exists. What am I doing wrong?

This app / controllers / config / assets.php

$file = FCPATH . 'application\config\assets_fe.php'; if(is_writable($file)) { $open = fopen($file, 'W'); if(!fwrite($open, $frontend)){ echo "Could not write file!"; } else { echo "File written!"; } } else { echo "File not writable!"; } 

This is a screenshot of the file at its location in Windows Explorer. config folder

I tried the following file paths in the write_file function:

 write_file(FCPATH.'application/config/assets_fe.php', $frontend, 'W'); write_file('application/config/assets_fe.php', $frontend, 'W'); write_file('./application/config/assets_fe.php', $frontend, 'W'); 

None of the above file paths worked ... maybe this is something else?

FYI: FCPATH = 'W: \ wamp \ www \ zeus \' and it is determined by the CI controller before returning the path to this file.

Update: I tried to use only my own version of PHP, and he said that the file or directory does not exist. Maybe I'm using the wrong way. What should it be?

+8
file php codeigniter
source share
3 answers

The answer is simple, you are passing an invalid mode, captical W , fopen . It must be lowercase w . So, what happens fopen returns false , then you pass this instead of fwrite instead of the resource, forcing it to return false .

Just change

 $open = fopen($file, 'W'); 

to

 $open = fopen($file, 'W'); 

You can also check fopen result

 if(!$open) echo 'Could not open file'; 

You can also try file_put_contents , since you are not doing anything with the file descriptor.

 $file = FCPATH.'application\config\assets_fe.php'; if(is_writable($file)){ if(!file_put_contents($file, $frontend)) { echo "Could not write file!"; } else { echo "File written!"; } } else { echo "File not writable!"; } 
+6
source share

You must use set_item to write to the configuration file.

 $this->config->set_item('item_name', 'item_value'); 

for arrays:

 $this->config->set_item('ftp_frontend', array(...)); 

Here it is in config .

0
source share

I do not think you need to write in the config.php file. Instead of writing in the config.php , the custom CI configuration class can override the value of the configuration file, as shown below

For one item

 $this->config->set_item('item_name', 'item_value'); 

For an array element

 $this->config->set_item('item_array', array(...)); 
0
source share

All Articles