php not creating file using fopen() - php

It is always giving the message as "not created". I have checked the permissions and its 777. What to do? It is running perfectly in command line mode.
this is the code:
<?php
$msg = "I'm a line that is a message.\n";
$path = $_SERVER['DOCUMENT_ROOT'] . '/myawesome_logfile.txt';
echo $path;
chmod($path, 0777);
$f = fopen($path, "a+"); //opening or creating the file
if($f)
echo "file created";
else
echo "not created";
fwrite($f, $msg); //writing the file
fclose($f);
?>

Related

PHP cron fwrite dont work

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";
}
?>

PHP fwrite doesn't update the file using the append mode

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";
}

cannot overwrite files with PHP

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!

fwrite not writing

$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... !!

Get a 0KB file after using PHP unlink() method

I'm trying to delete a file on the server. Below is the code I use.
function ServerDel($file){
$file = realpath($file);
echo ($file);
$fh = fopen($file, 'w') or die("can't open file");
fclose($fh);
if(unlink($file))
echo"Delete the file successfully.";
else
echo "Failed to delete.";
}
But after I run the code, the file still exists and becomes 0KB. Anyone knows how to get around this?
use a flag in fopen() instead of w.
$fh = fopen($file, 'a') or die("can't open file");
Try this:
function ServerDel($file){
$rfile = realpath($file);
echo ($rfile);
if (file_exists($rfile)) {
if(unlink($rfile)) {
echo "Delete the file successfully.";
} else {
echo "Failed to delete.";
}
} else {
echo "File does not exist";
}
}

Categories