$fp = fopen('log.txt', 'w');
fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . '\n');
The code above is not writing to the file. the $row['toolbar_id'] is a value from a for each loop. Any suggestions? There is no PHP error, as the file does open as I have debugged that part.
Try this for extra surety
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$fp = fopen('log.txt', 'ab');
if (false === $fp) {
throw new RuntimeException('Unable to open log file for writing');
}
$bytes = fwrite($fp, 'Missing gallery image for: ' . $row['toolbar_id'] . PHP_EOL);
printf('Wrote %d bytes to %s', $bytes, realpath('log.txt'));
fclose($fp);
Edit: Changed the "write" flag (w) to "append" (a) as truncating a log file doesn't sound like a great idea
https://bugs.php.net/bug.php?id=48607
there is a php bug with fwrite and ftp which means the last chunks of files sometimes dont get written when fclose is called directly after fwrite
putting a sleep(1); before fclose fixes the issue, or in your case logging to a file before fclose can also stop it
posting for future reference!
<?php
$filename = 'log.txt';
$somecontent = "Missing gallery image for: ";
if($row['toolbar_id'] != "")
{
$somecontent .= $row['toolbar_id'];
}
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
Try this code... !!
Related
if (is_writable($filename2)) {
if (!$handle = fopen($filename2, 'a')) {
echo "Cannot open file ($filename2)";
exit;
}
if (fwrite($handle, $datass) === FALSE) {
echo "Cannot write to file ($filename2)";
exit;
}
fclose($handle);
} else {
echo "The file $filename2 is not writable";
}
}
$filename2 = 'test.txt';
and this code work and the info come on test.txt but I want to process more files like test.txt and test2.txt.
How can I do this?
Not sure what you want to achieve, but maybe you want to extract a function and then use that function to write to multiple files?
function writeToFile($filename, $content)
{
if (!is_writable($filename2)) {
echo sprintf(
'The file "%s" is not writable',
$filename
);
return;
}
$handle = fopen($filename, 'a');
if (false === $handle) {
echo sprintf(
'The file "%s" cannot be opened',
$filename
);
return;
}
$written = fwrite($handle, $data);
fclose($handle);
if (false === $written) {
echo sprintf(
'Could not write to file "%s"',
$filename
);
}
}
$data = '...';
writeToFile('test.txt', $data);
writeToFile('test2.txt', $data);
Note Opening files with fopen() and mode a and then writing using fwrite() will append to existing files, otherwise create new files. Also, you should still close the file handle with fclose(), even if you were unable to write to the file.
For reference, see:
http://php.net/manual/en/language.functions.php
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fwrite.php
http://php.net/manual/en/function.fclose.php
I try this:
<?php
$time = time();
$fh = fopen("cron.txt", 'a+');
fwrite($fh, $time);
echo "ok";
?>
to run on SSH:
/opt/bitnami/php/bin/php /opt/bitnami/apache2/htdocs/cron.php
and remote server:
budzinski.xyz/cron.php
on localhost it works fine, on remote server and on cron, don't
your cron command needs to modified like this-:
/opt/bitnami/php/bin/php /home/[yourcpanelname]/[your patht to the file]/cron.php
check the permissions of file is it writable or not
Try out this example and than see if you are getting any output or not
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
The following code is working but it doesn't update the contents of the file it created.
I can see that the file contents have changed (the size increased) but when I download the file from my server it's empty.
the file is chmod to 666 and its parent directory as well.
its a linux server running Apache and PHP.
I've also tried using fflush to force it to flush the contents.
<?php
header("Location: http://www.example.com");
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, '=');
fwrite($handle, $value);
fwrite($handle, '\r\n');
}
fwrite($handle, '\r\n');
fflush($handle);
fclose($handle);
?>
what is the problem?
Thanks!
I think a good practice is to check if a file is writable with is_writable then if it can be opened by checking the value returned by fopen, by the way your code is right.
Try this:
$filename = "log.txt";
$mode = "a";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, $mode)) {
echo "Cannot open file ($filename)";
exit;
}
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, '=');
fwrite($handle, $value);
fwrite($handle, '\r\n');
}
fwrite($handle, '\r\n');
fflush($handle);
fclose($handle);
echo "Content written to file ($filename)";
} else {
echo "The file $filename is not writable";
}
I am trying to overwrite files i created in php
when i'm using file_put_contents i receive 0 of "x" bytes written
with the function below that uses fwrite i get the error the file is not writable.
the permissions are 777 (including the directories)
there is no problem creating files, the problem is when i try to modify them.
can anyone figure what is the problem?
function update_body($body)
{
$URL = $_REQUEST["URL"];
$filename = $URL;
$somecontent = $body;
if (is_writable($filename))
{
if (!$handle = fopen($filename, 'w'))
{
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE)
{
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
}
else
{
echo "The file $filename is not writable";
}
}
the procedure where i create the file
function save_body($body)
{
$datearr = getdate(); //get date array, use date for folder, time for filename
$current_date = $datearr['year'].$datearr['mon'].$datearr['mday'];
$current_time = $datearr['hours'].$datearr['minutes'].$datearr['seconds'];
// create directory according the date
if (!file_exists($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date))
mkdir($_SERVER['DOCUMENT_ROOT'] .'/articles/'.$current_date, 0777, true);
// write the file
$fp = fopen($_SERVER['DOCUMENT_ROOT'].'/articles/'.$current_date.'/'.$current_time.'.txt',"w");
fwrite($fp,$body);
fclose($fp);
return $current_date.'/'.$current_time;
}
Welcome to Stack Overflow.
As #Sebas said, try to use a relative path (../../../articles) or wherever your folder is instead of
$_SERVER['DOCUMENT_ROOT']
Have a nice day!
My file contains this content (1.txt):
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
user.php?id=XXXXXX
text/numbers
user.php?id=XXXXXX
user.php?id=XXXXXX
and so on ...
(XXXXXX = numbers)
It's possible to take only user.php?id=XXXXXX and copy them to another file, without coping unnecessary text because file contains about 50 000 lines ?
<?php
$file = fopen('source.txt', 'rb');
$newfile = fopen('target.txt', 'wb');
while(($line = fgets($file)) !== false) {
if(strpos($line, 'user.php') !== false) {
fputs($newfile, $line);
}
}
fclose($newfile);
fclose($file);
?>
This code hasn't been tested, but I think it will work properly.
<?php
//Get all the matches from the file
$fileContents = file_get_contents('1.txt');
preg_match_all('/user.php\?id=[0-9]{6}/', $fileContents, $matches);
//Output to new file
$fh = fopen('output.txt', 'w+');
foreach ($matches['0'] as $match) {
fputs($fh, $match."\r\n");
}
fclose($fh);
?>
<?php
error_reporting(0);
$fs=fopen("1.txt", "r");
$ft=fopen("2.txt", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
//echo "File Handling successfully ...";
?>
suppose you want to copy the contents of "abc.php" file to "working_file.php"
place the following code in working_file.php , it will simply copy all the content.
<div>
<?php
$file = fopen("abc.php", "r") or die("Unable to open file!");
echo fread($myfile,filesize("abc.php"));
fclose($file);
?>
</div>