Jump to content

Recommended Posts

Hello. I have a quick question (it's probably easy to solve): why isn't the below code working?

 

<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w+');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
?>

 

All I get is a blank page. Nothing is displayed, and I'm not sure what to do.

Link to comment
https://forums.phpfreaks.com/topic/272021-file-contents-wont-echo/
Share on other sites

Your file does not contain any data to be read.  Even if you put data into it before running the script, it won't have any in it anymore by the time it reaches the fread call due to:

'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

The the manual page for fopen for details on what mode to use and when.

 

Your file does not contain any data to be read. Even if you put data into it before running the script, it won't have any in it anymore by the time it reaches the fread call due to:

 

The the manual page for fopen for details on what mode to use and when.

 

When I woke up today morning, I looked over the code and realized I forgot to include any text in the file; I must have been really tired yesterday. Anyway, I changed the fread attribute, like you recommended, but the code still isn't being displayed:

 

<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a+');
fwrite($fh, "Hello World!");
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
?>

Edited by BorysSokolov

Try again fopen

 

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

 

Hint: If you want to read data from a file, you need to start somewhere before the end of all of the data in the file.

 

And, since you are not writing to the file in that code, you don't need to open it "for writing", just "for reading"

Try again fopen

 

 

 

Hint: If you want to read data from a file, you need to start somewhere before the end of all of the data in the file.

 

And, since you are not writing to the file in that code, you don't need to open it "for writing", just "for reading"

 

Right, that's a stupid mistake on my part. Works fine now, though. Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.