chandler Posted October 20, 2010 Share Posted October 20, 2010 Hi heres what I would like to do, im sure its not the best way to do it but its the only way I have used in the past . getting content string <?php $file = file_get_contents('contents.txt', 'r'); echo $file; ?> usualy I would use a edit link with this code <?php if(session_is_registered(myusername)){ ?><a href="javascript:open1()"><div id="edit_link">Edit</a></div><?php } ?> But what I would like to do now is to skip the edit link and just make the content string a link once the user is logged in they will be able to click on the content string and it will activate the javascript:open1() Many thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/ Share on other sites More sharing options...
chandler Posted October 20, 2010 Author Share Posted October 20, 2010 if i do this <?php $file = file_get_contents('contents.txt', 'r'); echo $file; ?><?php if(session_is_registered(myusername)){ ?><a href="javascript:open4()"><?php echo $file ?></a><?php } ?> then I get the content of the file printed twice on the web page Please help...thanks Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124479 Share on other sites More sharing options...
The Little Guy Posted October 20, 2010 Share Posted October 20, 2010 what you want is fread $filename = '/path/to/file.txt'; $handle = fopen($filename, 'rb'); $file = fread($handle, filesize($filename)); fclose($handle); if(session_is_registered(myusername)){ echo '<a href="javascript:open4()">'.$file.'</a>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124484 Share on other sites More sharing options...
chandler Posted October 20, 2010 Author Share Posted October 20, 2010 maybe im doing somthing wrong but that didnt work, when I do that I am only able to see the content when logged in. Here is the whole code: header.php <?php //This condition will check either the submit button was pressed, for such test you need to check that if there is an element which has the same name as your button. if(isset($_POST['Submit'])) { //this is just the name of the file, it can be used directly but its better to use a variable its just for convinence $file = "../content.txt"; if(is_writable($file)) { //this is the open the file in write more 'w' says that it will open in writeable more $fh = fopen($file, 'w') or die("can't open file"); //I again used a variable to store the data.... which is to be written to the file //The main point is to receive the data on submit, it is when submit is done, and uppen condition for submit is fulfilled //all the elements of the form are put in the array named $_POST and you can get their values by their names //in the same way I just took the value of 'editor1' it is the name for textarea in which you displayed the text $data = stripslashes(stripslashes($_POST['header'])); ///This routine will just write the data to file given the file handle and the data. fwrite($fh, $data); //must close the file after all writing is done. fclose($fh); } else { echo "File is Not Writable<br>"; } } ?> <form method="post"> <textarea name="header"> <?php $file = file_get_contents('../content.txt'); echo $file; ?> </textarea> Then this to call the file and display it. <?php $file = file_get_contents('content.txt', 'r'); echo $file; ?> then this calls header.php that allows me to edit the content, only when logged in. <?php if(session_is_registered(myusername)){ ?><a href="javascript:open1()"><div id="edit_link">Edit</a></div><?php } ?> Im a complete noob at this a friend gave this to me and showed me how to set it up I just need to make some small modifications to it now... Remove edit link and make the content sting active so once clicked on, while logged in it will open the textarea for editing. many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124507 Share on other sites More sharing options...
chandler Posted October 20, 2010 Author Share Posted October 20, 2010 any idea anyone please? Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124568 Share on other sites More sharing options...
The Little Guy Posted October 20, 2010 Share Posted October 20, 2010 Try changing this: echo $file; to this: echo htmlentities($file); Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124570 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 it looks like you're seeing the content on the page twice because you're outputting it twice. The first time - echo $file Is your first output, then if you're logged in, the second one will output as well... Perhaps remove the first echo $file from your code after you read the file. Also, fread as someone mentioned earlier is a way to do it, but I think you're on track with file_get_contents as it returns the contents into a string without the need to use all the extra functionality as described with fread. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124571 Share on other sites More sharing options...
chandler Posted October 20, 2010 Author Share Posted October 20, 2010 Still stuck on this it seems whatever I try either removes the content or you have to be logged in to view it or it displays it twice while logged in. please post if you have any more ideas, Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124587 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 Are you still echoing it twice? Show us your latest code. Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124592 Share on other sites More sharing options...
chandler Posted October 20, 2010 Author Share Posted October 20, 2010 <?php $file = file_get_contents('content/menu_header_a.txt', 'r'); echo $file; ?><?php if(session_is_registered(myusername)){ ?><a href="javascript:open1()"><?php echo htmlentities($file); ?></a><?php } ?> also tried this <?php $file = file_get_contents('content/menu_header_a.txt', 'r'); echo htmlentities($file); ?><?php if(session_is_registered(myusername)){ ?><a href="javascript:open1()"><?php echo $file; ?></a><?php } ?> If I dont echo it here then I don't see any content. <?php $file = file_get_contents('content/menu_header_a.txt', 'r'); echo $file; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124597 Share on other sites More sharing options...
BlueSkyIS Posted October 21, 2010 Share Posted October 21, 2010 I don't know how you can tell what is going on with every thing coming in and out of PHP on one line like that. <?php $file = file_get_contents('content/menu_header_a.txt', 'r'); if (isset($_SESSION['myusername']) && $_SESSION['myusername'] > '') { echo "<a href='javascript:open1()'>".htmlentities($file)."</a>"; } else { echo htmlentities($file); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124680 Share on other sites More sharing options...
jcbones Posted October 21, 2010 Share Posted October 21, 2010 Try: <?php $file = file_get_contents('contents.txt', 'r'); if(session_is_registered(myusername)){ ?><a href="javascript:open4()"><?php echo $file ?></a><?php } else { echo $file; }?> Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124718 Share on other sites More sharing options...
chandler Posted October 21, 2010 Author Share Posted October 21, 2010 great thanks for that jcbones works great. Thanks for the help everyone. Quote Link to comment https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/#findComment-1124829 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.