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);
Related
I've been using file_put_contents in order to create txt files inside a specified folder that has write permissions:
file_put_contents($dir.$file.'.txt', $content);
Editing my code, I made a mistake: I wrote $dir = '/../../xxx/yyy/'; (that actually doesn't exist) instead of $dir = '../xxx/yyy/'; (right directory).
Obviously, no file has been created (all other folders are read-only), but I didn't get any error message about it.
Why?
P.S.: I get other error messages on the same PHP page, but not the above one.
From the docs:
"This function returns the number of bytes that were written to the file, or FALSE on failure"
https://www.php.net/manual/en/function.file-put-contents.php
I.e use something like
$result = file_put_contents($dir.$file.'.txt', $content);
And check if it's true or not
try this
$dir = '../xxx/yyy/';
$handle = fopen($dir,'w');
fwrite($dir, "write some here");
fclose($dir);
Here is a snippet of what I'm trying to do:
$file = fopen($path, "wb");
fwrite($file, $data);
fclose($file);
Simple enough.
But when I open the created file, I see 0x0D inserted before 0x0A everywhere. I understand that this will happen if I open the file without binary mode.
But I've clearly specified I want binary mode. Maybe my brain isn't functioning right or something, so.. Anyone got a solution?
It turns out, for some weird reason, the problem was with my $path. My $path value was "temp".
It would generate the file named "temp" but would refuse to open it in binary mode. Giving the file an extension like "temp.bin" or "temp.tmp" allowed it to work in binary mode.
Problem solved for now but I'm still wondering why it works like this.
Seems the problem is with the $path. Please make sure you have given the correct file path.
If you are defining the $path with a dynamic file name, use / before the file name. For example, $var = "/var/www/html/projectFolder/folderFile/". "Filename.fileformat"
If you're working with URLs in a redirection context, then the root directory ('/') refers to your domain's root. The same goes for paths for linking files or images and for include and require directives.
You're making the classic mistake of confusing data with the representation of that data.
Let's say you have a text file. If you open it in Notepad, you'll see the following:
$str = "Hello world!";
echo bin2hex($str); // output: 48656c6c6f20776f726c6421
$file = fopen($path, "wb");
$data = bin2hex($data);
fwrite($file, $data);
fclose($file);
I have one image in a directory on my server, and want to copy it to another directory.
So I'm using
$post_picture = 'http://mysite.com/image.jpg';
copy($post_picture,
'images/pictures/post/thumb/' .
$info['filename'] .
'_thumb.' .
$info['extension']);
The issue is that in fact a file is created in my thumb directory, but that image is empty (0 x 0 pixels). I get no errors.
Any idea what is happening?
Permissions on all dirs are 755, both original and copy image have 644. The original show normally on a browser.
Thanks.
Do you have any form of hot-link protection that could alter what php receives?
Is allow_url_fopen allowed?
The $post_picture variable should probably have the file system path to the file, rather than the URL to the file.
Is allow_url_fopen set to true in your php.ini?
Sometimes that can produce this result if remote connections are being blocked.
$post_picture should be a local path i believe
copy( '/path/image.jpg', ... );
Please use this one...
<?php
$source = 'f-1/2.jpg';
$destination = 'f-2/2.jpg';
$data = file_get_contents($source);
$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>
I am able to put the file contents if I stay in the same directory level
ie: `file_put_contents('cache.txt', $result);
But how would I put the contents up a folder, or even two? Adding '../cache/cache.txt' does not seem to work.`
That would work fine assuming the user the web server runs as has write permission to that directory.
Try using the direct path of where you would like to put the cache content.
<?php
$file = 'people.txt';
$cacheFile = __FILE__ . '/cache/'. $file;
$content = 'Something';
file_put_contents($file, $content);
file_put_contents($cacheFile, $content);
?>
Assuming you have a folder called cache, with write permissions to it.
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);