Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/216386-help-with-file_get_contents/
Share on other sites

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

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>';
} 
?>

 

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.

 

 

 

 

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!

<?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; ?>

 

 

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);
}
?>

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.