manalnor Posted July 9, 2010 Share Posted July 9, 2010 Hello friends, i've an problem. if i've an text file (hi.txt) and want to open it within php file , i've write that code. <textarea> <? $fn = "hi.txt"; // path name of text file print htmlspecialchars(implode("",file($fn))); ?> </textarea> it will show the content of (hi.txt) Now the problem if i deleted the text file (hi.txt) it will shows me stupid error so can someone kindly rewrite that code to be more improved so that it act as following :- if hi.txt is exist in the same path then it will show its content if hi.txt is not exist or 0 byte .. then it won't show the textarea and give me for example an message " THERE IS NO FILE " thanks. Quote Link to comment Share on other sites More sharing options...
xcandiottix Posted July 9, 2010 Share Posted July 9, 2010 <? $fn = "hi.txt"; // path name of text file if($fn){ print htmlspecialchars(implode("",file($fn))); } else{ echo "no file found"; } ?> Maybe? Or it might be something like if($fn != NULL){ or if(isset($fn)){ Quote Link to comment Share on other sites More sharing options...
manalnor Posted July 9, 2010 Author Share Posted July 9, 2010 thank you for your try but it won't work..here is another try from me. <? if () { echo "<center> NO FILE </center>"; } else { echo "<textarea rows='5' name='txt_content' cols='40'>"; $fn = "hi.txt"; print htmlspecialchars(implode("",file($fn))); echo "</textarea>"; } ?> the missing part here is (if) lol if what ? it should be if hi.txt not exist ... how to write it in php Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 I'm not really sure why you're using the file() function within the implode() function to pull the contents of a file and put it into a string. The function file_get_contents() does this already. <?php $filename = '/path/to/hi.txt'; // Check to make sure the file exists // If it does, then continue, otherwise produce an error if (file_exists($filename)) { // Get the entire contents of the file as a string $contents = file_get_contents($filename); // Populate the text area with the contents of the file echo '<textarea>' . htmlspecialchars($contents) . '</textarea>'; // Is htmlspecialchars() strictly necessary?! } else { echo 'The file <strong>' . $filename . '</strong> does not exist.'; } ?> Quote Link to comment Share on other sites More sharing options...
manalnor Posted July 9, 2010 Author Share Posted July 9, 2010 thanks you so much... it works now perfect Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.