I was wondering if there is any way to use fwrite() to create an .md file. I am creating a blog for a team that needs to upload content without me typing it out. The content is in markdown via an .md file for each post. I am trying to make a web page that will take content and create an .md file with it. Right now I just have a proof test running and am trying to save this to a .md file. However, when I try to change the extension to .md I keep getting errors. I'm kinda a PHP noob so your help is apricated.
TLDR
I'm wondering why this (fwrite("newfile.md", "w") or die("Unable to create file!") would throw an error while using .txt won't.
CODE
<?php
$myfile = fwrite("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Your issue would be that you aren't opening/creating a file, you're just attempting to write to it. You need to use fopen() before you write to it:
$myfile = fopen("newfile.md", "w") or die("Unable to create file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
Related
I have this php code in Bluemix server:
<?php
include "config.php";
$myfile = fopen("uploads/ac/1.txt", "a+") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
When I access this php page via url, it shows me this error :
Unable to open file!
I guess its something to with options.json file or permissions but can't figure out what.
The same code works fine on localhost. Please help.
so i have this code for generate file.txt
<?php
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
then i show it on textarea like this:
<textarea rows="5" cols="50">
<?php
$file = file_get_contents('./test.txt', FILE_USE_INCLUDE_PATH);
echo $file;
?>
</textarea>
i need to print the file test.txt directly using button in php. How can i do this in php / laravel framework?? thanks
1 instead of showing the textarea, just show a link:
print text.txt!
2 create a new file called print_test_txt.php and populate it with this php
3
<?php
$file = file_get_contents('./test.txt', FILE_USE_INCLUDE_PATH);
echo $file;
?>
which will output it to the screen so the user can print it. if you are hoping to print to a known printer, see this solution:
directly print with php
I want to create a PHP file through WordPress dashboard without using ftp/c-panel, as I have checked and I found that you can create a PHP file through adding code in header.php But I don't have header.php in child theme as well and no access to cPanel. Any suggestion how I can create php file from WordPress dashboard appearance->editor by putting some code in functions.php?
Thank you in advance.
Put this code in your function.php file and run the site
add_action( 'init', 'createnewfile' );
function createnewfile()
{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
}
I have modified the answer to be able to:
The function only runs when the theme is "activated".
And, if for some reason the file exists, don't overwrite the existing one.
function createnewfile() {
$filename = dirname(__FILE__)."/newfile.php";
if (file_exists($filename)){
//file exists, then it does nothing.
} else{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "Tienes que editar este archivo2\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
add_action( 'after_switch_theme', 'createnewfile' );
How can I run a cron task in Linux?
Following this Q&A,I have this cron task to run - just writing some info to a txt file,
// /var/www/cron.php
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
But after adding the cron task via my terminal,
crontab -e
and then,
* * * * * /usr/bin/php /var/www/cron.php &> /dev/null
But when I open cron.txt, there is nothing in it.
Why? What have I missed?
Change cron.txt by full path /var/www/my_system/cron.txt
// /var/www/cron.php
$myfile = fopen("/var/www/my_system/cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
Or move to directory:
chdir("/var/www/my_system");
$myfile = fopen("cron.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
And try again.
I would eliminate the redirect to /dev/null until you're sure you're not getting an error message.
My guess is "permissions".
SUGGESTIONS:
Execute /usr/bin/php /var/www/cron.php manually, from the command prompt, to make sure the PHP script is OK.
Identify the directory "myfile.txt" is being written to.
Make sure both the directory and myfile.txt are writable.
Here are a couple of links with other troubleshooting hints:
So, your cronjob did not run?
http://www.thegeekstuff.com/2009/07/linux-unix-crontab-cronjob-issue/
I tried this but wont work
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
fwrite($myfile, $txt);
$txt = "Minnie Mouse\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
This is how it displays in my newfile.txt:
Mickey MouseMinnie Mouse`
but I want it like this:
Mickey Mouse
Minnie Mouse
It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character(s) for the platform PHP is running on.
$txt = "Goofy".PHP_EOL;
\r\n
add the carriage return in front.
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Mickey Mouse\n";
file_put_contents($myFile,$txt ,FILE_APPEND);
fclose($myfile);
?>
if you want write \n in file use from file_put_contents($File_Handle,$Text_For_Write,FILE_APPEND)