I intend to save all views files generated by Codeigniter on my desktop; I need to output the views into files like home.html, single.html, page.html inside a folder. Is there an solution for this?
Actually you might return a view as data, simply assign it to a variable.
$string = $this->load->view('myfile', $data, true);
Just get this response, create a file and upload to the server (using either fwrite or ftp).
Below a quick example:
<?php
$file = '/var/www/whatever/upload_dir/file.html';
$string = $this->load->view('myfile', $data, true);
// Write the contents back to the file
file_put_contents($file, $string );
?>
Hope it helps!
You can load the view and have it returned as a string by supplying a third parameter (boolean TRUE) while calling $this->load->view(). From there, you can call file_put_contents() to write the string into a file.
$view_string = $this->load->view('some_view', '', TRUE);
file_put_contents('filename', $view_string);
You can also use write_file() supplied by CodeIgniter File Helper to write the string to a file.
Related
I have the contents of a file in a string. I need to pass this file to a function where the function is expecting the parameter to be the name of the file, not the contents. The obvious and probably simplest way to do this would be to write the contents to a temp file, then pass that file name to the function, and unlink the file once I'm finished.
However, I'm looking for a solution that doesn't involve writing the file out to the file system and then reading it back in. I've had a need for this in multiple cases, so I'm not looking for a work-around to a specific function, but more of a generic method that will work for any function expecting a file name (like file_get_contents(), for instance).
Here are some thoughts, but not sure how to pursue these yet:
Is it possible to write the contents somewhere in memory, and then
pass that to the function as a filename? Perhaps something using
php://memory.
Is it possible to write the contents to a pipe, then pass the name of the
pipe to the function?
I did a short proof-of-concept trying with php://memory as follows, but no luck:
$data = "This is some file data.\n";
file_put_contents( 'php://memory', $data );
echo file_get_contents( 'php://memory' );
Would be interested in knowing of good ways to address this. Googling hasn't come up with anything for me.
It mainly depends on what the target function does with the file name. If you're lucky, you can register your own stream wrapper:
stream_wrapper_register('demo', 'DemoStream');
$data = "This is some file data.\n";
$filename = 'demo://foo';
file_put_contents($filename, $data );
echo file_get_contents($filename);
Why not use a file in the /tmp/ directory? Like this:
<?php
$filename = '/tmp/mytmpfile';
$data = "This is some file.\n";
file_put_contents($filename, $data);
$result = file_get_contents($filename);
var_dump($result);
Well, as you say you don't want to use a file, you shouldn't use file_get_contents().
But you can achieve the same result by using stream_get_contents(), like this:
<?php
$data = "This is some file data.\n";
$handle = fopen('php://memory', 'r+'); // open an r/w handle to memory
fputs($handle, $data); // write the data
rewind($handle); // rewind the pointer
echo stream_get_contents($handle); // retrieve the contents
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$log = base_path()."/storage/logs/trade.log";
if ( !file_exists ($log) ) {
$data = fopen($log, "w");
}
file_put_contents($log, $data . PHP_EOL.PHP_EOL, FILE_APPEND);
I have a page use file_put_contents to record log. when my folder don't exist file, it will auto create the file and add log into it
My problem is when first time auto create file and place content into it. It comes out - Resource id #233
2nd time without crate file will be normal no any problem
anyone know how to fix this?
fopen() returns a resource.
file_put_contents() write your file and converts $data into a string (calls __toString() on the resource).
The second times, the file exists. So, the program doesn't pass into the if-condition and it writes $data that contains JSON.
To solve your problem, just remove your if block.
Documentation said :
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
Good Day All
I have a .php file which I want to edit via fopen() and file_get_content() functions. However, my file contains some php codes as well and I managed to get the content out of my file but without the php part. Also, I have tried the eval() (I know it's not suggested!) function with same results. I was wondering if there could be a way to get whatever is inside that file regardless wether it's text or codes.
Thanks
Here is the code I used:
public function editwarning()
{
$filename = "http://www.parkho.ir/admin/templates/pm/email_warning.php";
$content = file_get_contents($filename);
echo $content;
}
You have two options:
1) pass the file PATH to the $filename var:
$filename = "/var/www/app/email_warning.php"; // <--- replace /var/www/app for your path
2) Or You need to use htmlentities():
<?php
$content = htmlentities(file_get_contents($filename));
echo $data;
I wanted to know how can I do something like that:
To have a file named lang.json
to put it anywhere in my laravel directory
when the user enter to public/lang.json wrap the content inside this file in JSON.parse() ?
I know hot to do it in pure php
Just to readfile and then wrap it with this command and than output it to a user.
But how can I achive that behvior with laravel ?
There we have a File class to deal with files so in order to get contents of a file you just need to call in your controller:
$file_contents = \File::get('some_file.json');
$decoded = json_decode( \File::get('some_file.json') ); // or this way
also you may find usefull such method as
\File::exists('some_file.json')
Finaly:
if(\File::exists('some_file.json'){
$decoded = json_decode( \File::get('some_file.json') );
}
Note that it gets relative to public directory path, so when you give it 'some_file.json' it expects it to be placed in public/some_file.json
Fast to explain, but I can't get it to work:
In this simple code, the function force_download simply doesn't make any output.
$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data);
echo $data."/".$filename;
Here I just get a white screen, but the file content is show (well you know, the strange codified content :)
I think it is simple enough, I just want the file downloaded with no other effect, am I doing something wrong?
This will work with you
$this->load->helper('download');
$path = file_get_contents(base_url()."modulos/".$filename); // get file name
$name = "sample_file.pdf"; // new name for your file
force_download($name, $path); // start download`
Just a note to anyone else who may be having this problem: Make sure you have a file extension on the filename you supply for the first argument to force_download().
CodeIgniter uses this to set the MIME type, and it doesn't seem to work without.
Remove that echo $data."/".$filename; It should be like this
$this->load->helper('download');
$data = file_get_contents("modulos/".$filename); // Read the file's contents
force_download($filename, $data);
You should not call function after force_download(), Just remove the last line.
remove base_url() and do like this
$path= file_get_contents('./uploads/abc.jpg);