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";
}
}
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";
}
?>
I want to write a script that checks if the file exists on the remote Server or not.
If file exists Write url in a file found.txt. How i can Read many urls from a txt file and Write the url to a different txt file?
This is my code:
<pre>
$sourcePath = "http://www.example.net/file.txt";
$AgetHeaders = #get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
</pre>
If you store urls in text file one per line you could manage it like this:
$handle = fopen("AddressList.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$sourcePath = $line;
$AgetHeaders = #get_headers($sourcePath);
if (preg_match("|200|", $AgetHeaders[0])) {
$found = fopen('found.txt', "w") or die("Unable to open file!");
fwrite($found, $sourcePath);
fclose($found);
} else {
echo "Not found!";
}
}
} else {
// error opening the file.
}
fclose($handle);
I'm trying to write a nickname from teamspeak to a file.
It goes through all the checks, but when I check the file afterwards, nothing gets written.
$file = "test.txt";
$fh = fopen($file, 'w');
if(file_exists($file))
{
echo "exist";
}else{
echo "absent";
}
if(is_writable($file))
{
echo "writable";
}else {
echo "cant write";
}
if (false === $fh) {
throw new RuntimeException('Unable to open log file for writing');
}
foreach($Summoners as $su){
$name = $su["TsNICK"];
echo $name;
fwrite($fh, $name);
echo "Wrote to file";
}
fclose($fh);
I can't really tell why your code isn't working, but you should consider rewriting it to this:
$file = "test.txt";
if (false === ($fh = fopen($file, 'w'))) {
throw new RuntimeException('Unable to open log file for writing');
}
foreach($Summoners as $su) {
$name = $su["TsNICK"];
echo $name;
fwrite($fh, "$name\n");
}
fclose($fh);
If you wish to always append to a file, you would need mode 'a' instead of 'w'.
$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... !!