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);
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
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 have visited this article previously and found it useful, but i would like to add more functionality to it by having it save an image file name according to the URL name.
This is what I've done so far and it works.
$contents=file_get_contents('http://www.domain.com/logo.png');
$save_path="C:/xampp/htdocs/proj1/download/[logo.png]";
file_put_contents($save_path,$contents);
Basically, where I have put square brackets around I want to have that dynamic based on the URL file name. For example, if i have an image url such as this: https://cf.dropboxstatic.com/static/images/brand/glyph-vflK-Wlfk.png, I would like it to save the image into the directory with that exact image name which in this case is glyph-vflK-Wlfk.png.
Is this possible to do?
I would do this way
$url = "http://www.domain.com/logo.png";
$file = file_get_contents($url);
$path = "C:/xampp/htdocs/proj1/download/". basename($url);
return !file_exists($path) ? file_put_contents($path, $file) : false;
From what I understand, what you're trying to do is the following :
$url = 'http://www.domain.com/logo.png';
$contents=file_get_contents($url);
$posSlash = strrpos($url,'/')+1);
$fileName = substr($url,$posSlash);
$save_path="C:/xampp/htdocs/proj1/download/".$fileName;
file_put_contents($save_path,$contents);
There is a function for that, basename():
$url="http://www.domain.com/logo.png";
$contents=file_get_contents($url);
$save_path="C:/xampp/htdocs/proj1/download/".basename($url);
file_put_contents($save_path,$contents);
You might want to check if it already exists with file_exists().
I have a simple code snippet
$data = '[' . date('d-m-Y H:i:s') . ']: Operation timeout at URL ' . $this->curlInfo['url'];
print $data;
file_put_contents('/logs/curl_timeout.txt', $data);
The print does the following
[01-08-2014 09:42:05] Operation timeout at URL http://example.com
I want to save this to the file curl_timeout.txt located under logs. Both the file and the folder have 777 rights. However it is still not working. No text gets saved there. Am I doing sth wrong?
Edit : I also tried opening it first
fopen('/logs/curl_timeout.txt', 'wb'); But it still doesn't save the text to the file.
You specify your file path wrongly: /logs/curl_timeout.txt', $data); I mean leading slash,
in that case you should specify absolute path like: /var/www/public_html..` etc..
So use relative path: maybe ../../logs/curl_timeout.txt', $data); also there may be permissions problem check it also
Solution would be:
to define log path define('LOG_PATH', '/var/www/...log/...')
than use it like : file_put_contents(LOG_PATH, $data);,
also if you have tmp log directory one level up you can try: file_put_contents(LOG_PATH."../tmp/log/...", $data);
Seems like problem with the location and file it seems.
Did you check whether that file exists or not ?
Try printing out things from that file using file_get_contents first.
Hope this helps.
Cheers!
First of all check return value:
$result = file_put_contents('/logs/curl_timeout.txt', $data);
var_dump($result);
If $result === FALSE then you have a problems. Try to do something like this:
$logfile = realpath('./logs/curl_timeout.txt');
$result = file_put_contents($logfile, $data);
var_dump($result);
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.