user4767557
user4767557

Reputation: 13

PHP File Writing shows Empty Screen

I am trying to write some content infile but it is showing blank screen

$write = fopen("newfile.txt", "r");
fwrite($write, "John Doe");

Here is my code , what is the problem ,

Upvotes: 1

Views: 56

Answers (2)

Ghostman
Ghostman

Reputation: 6114

use

$fh = fopen($myFile, 'a')

example

$myFile = "newfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "John Doe 1\n";
fwrite($fh, $stringData);
$stringData = "John Doe 2\n";
fwrite($fh, $stringData);
fclose($fh);

output

Floppy Jalopy
Pointy Pinto
John Doe 1
John Doe 2

Upvotes: 1

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

You have got the wrong action

Change the 'r' to 'w'

$write = fopen("newfile.txt", "w");
fwrite($write, "John Doe");

But i would recommend you to handle with die

<?php
$write = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($write, "John Doe");
fclose($write);
?>

Update : As the questioner wants to append with old content

Then you need set with a in your fopen

<?php
$write = fopen("newfile.txt", "a") or die("Unable to open file!");
fwrite($write, "John Doe");
fclose($write);
?>

Note : It is better to use \n at the end of your content as it will break the line and take it to next line, else the content will continue one the previous line itself

I would recommend you to read on php fopen

Upvotes: 0

Related Questions