Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a php script.
I want to open a file (if it doesn't exist, then create a new file) from my server and show the content in a textarea.

My code is,
XML
<textarea name="text" rows="25" cols="100">
<?php
                $fn=$SERVER['DOCUMENT ROOT'] . '/nink/hai.txt';
                $fh=fopen($fn,"r");
                fclose($fh);
                echo file_get_contents($fn);
            ?>
</textarea>


When I reloaded the page the textarea is filled with,
XML
<br />
<b>Warning</b>:  fopen(/nink/hai.txt) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory in <b>/home/www/imwazir.net/nink/index.php</b> on line <b>23</b><br />
<br />
<b>Warning</b>:  fclose() expects parameter 1 to be resource, boolean given in <b>/home/www/imwazir.net/nink/index.php</b> on line <b>24</b><br />
<br />
<b>Warning</b>:  file_get_contents(/nink/hai.txt) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in <b>/home/www/imwazir.net/nink/index.php</b> on line <b>25</b><br />


I changed the permission of the "nink" folder even to 777, but still I'm getting the error.
What I did wrong ? What should I do ?

Thanks.
Posted

1 solution

It should be
$_SERVER['DOCUMENT_ROOT']

which will give you www root path, is that where the "nink" folder is?
If the nink folder is located in the same folder as the php script, then
./nink/hai.txt 

is suffice.
XML
<textarea name="text" rows="25" cols="100">
<?php
$path = "./nink/hai.txt";
$myfile = fopen($path, "r") or die("Unable to open file!");
echo fread($myfile,filesize($path));
fclose($myfile);
?>
</textarea>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900