Mohsin
Mohsin

Reputation: 179

How to create file inside views folder in codeigniter

I am trying to create php file using codeigniter, I want to create that file inside views folder or inside controller/ model dynamically. But I am getting error Message: fopen(application/views): failed to open stream: No such file or directory. Looks like kind of restriction from codeigniter. How can I achieve this task ?

My Code:

        $file_location = APPPATH . "views"; 

        $myfile = fopen($file_location, "newfile.php", "w") or die("Unable to open file!");
        $txt = "<?php\n ";  
        $txt .= "echo 'something';\n" ;
        $txt .= "?>\n ";

        fwrite($myfile, $txt);
        fclose($myfile);

Upvotes: 1

Views: 2738

Answers (1)

IsThisJavascript
IsThisJavascript

Reputation: 1716

No this is a PHP issue I believe, not a code igniter.
See this line here:
fopen($file_location, "newfile.php", "w")

From the docs:
resource fopen ( string $filename , string $mode [, bool $use_include_path = FALSE [, resource $context ]] )

So change your fopen too:
fopen($file_location. "newfile.php", "w")
(I changed the , to a . to construct the file path)

Edit: make sure your $file_location ends with a / or add one to the start of newfile.php

Upvotes: 1

Related Questions