PHP fopen not working in IBM Bluemix - php

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.

Related

How to write a html file using PHP?

I used below HTML code and php code to Write new files.
<html>
<body>
<form action="write.php" method="get">
ID : <input type="text" name="ID"><br>
<input type="submit">
</form>
</body>
</html>
<?php
$myfile = fopen($_GET["ID"] , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
?>
If I submit TEST to html form , php code writes a file named "test". it hasn't a file extension.
How to write "TEST.html" with above code.?
Add the extension to the filename before opening the file:-
$filename = $_GET["ID"] . '.html';
$myfile = fopen($filename, "w") or die("Unable to open file!");
Note You are opening up yourself to all kinds of security issues here.
just use this code:-
fopen($_GET["ID"].'.html' , "w")
You need to add .html with a !empty() check there, like below:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html" , "w") or die("Unable to open file!");
$txt = "Your user ID =";
fwrite($myfile, $txt);
$txt = $_GET["ID"];
fwrite($myfile, $txt);
fclose($myfile);
}
?>
To open and read this file:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$myfile = fopen($_GET["ID"]."html", "r") or die("Unable to open file!");
echo fread($myfile,filesize($_GET["ID"]));
fclose($myfile);
}
?>
OR:-
<?php
if(!empty($_GET['ID'])){ // check first that ID is coming or not
$file = fopen($_GET["ID"]."html","r") or die("Unable to open file!"); ;
while(! feof($file)){
echo fgets($file). "<br />";
}
fclose($file);
}
?>
Note:-
In my opinion you need .txt rather than .html.An HTML file with text like test really have no mean, or server any useful purpose. BTW it's up-to-you what extension you want. I just gave my opinion.

print generated file.txt using php / laravel

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

Linux cron task - how to add and run a cron task?

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/

Use fwrite to create an .md file

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);

Making new line in php write to file

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)

Categories