Hi,
I have file into which I am getting feedbacks from users. But when the file is open, users can not give feedback because it does not write into it. Worse of all, Plus it gives Warning erros on users browser. So the code has to check if the file is inuse or opened currently, and inform user accordingly. How do I do that in PHP?

Thanks. MOL

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

@molla.getachew

I have file into which I am getting feedbacks from users. But when the file is open, users can not give feedback because it does not write into it. Worse of all, Plus it gives Warning erros on users browser. So the code has to check if the file is inuse or opened currently, and inform user accordingly. How do I do that in PHP?

It's an interesting question can you post a code so we can understand you are trying to do. If you don't have a code then my advice google it and look for that code. If you have a code then post it so we can look at it.

Something like this!

<?php

$name=  strip_tags(trim($_POST['name']));
$comment=  strip_tags(trim($_POST['comment']));

//echo $comment;

if($name !="" && $comment !="")
{
    if(!is_numeric($comment) and !is_numeric($name))
    {

    $userComment=date("Y-m-d H:i:s").',  '.strip_tags($name).', '. $comment."\r\n";

    $source='../files/filepad01.doc';
    clearstatcache();
    if(is_writable($source))//Note: JUST TRIED IF THIS FUNCTION SOLVES IT, BUT DOEN'T.
    {
        $file= fopen($source, 'a');
        fputs($file, $userComment);
        fclose($file);

        echo "<script type='text/javascript'>alert('Successful. Thank you for the feedback!');
            window.location.href='http://localhost:8081/popJamk/examplates/htmlpages/examfeed01.php';</script>";

    }  
 else {
        echo "<script type='text/javascript'>alert('File is not writable. Please try again.');
            window.location.href='http://localhost:8081/popJamk/examplates/htmlpages/examfeed01.php';</script>";;
    }

    }
 else {$htmlvar= "<html>
            <head></head>
            <body>            
            <a href=http://localhost:8081/popJamk/examplates/htmlpages/examfeed01.php><b>here.</b></a>
            </body>
        </html>";            
        echo die("Sorry your input is incorrect! To go back click ".$htmlvar);                 

        }
}
 else 
     {
        echo die("Sorry, Can not Process.");
     }    
?>

SOMETHING LIKE THIS!

Member Avatar for LastMitch

@molla.getachew

I read your code.

There's 2 option:

1) You need a query to select the info from the database Another words if the username write stuff in the comment. You can used a query to fetch that info from the database.

2) You need ajax to fetch the php file not using:

"<script type='text/javascript'>alert('File is not writable. Please try again.');
window.location.href='http://localhost:8081/popJamk/examplates/htmlpages/examfeed01.php';</script>";;.

Plus you have an extra ; semi-colon on the code above

i think you required a lock when file is open.and wait for while.
then again you can write into same file.

please goto following and use flock() method.

http://in1.php.net/flock

Member Avatar for diafol

If you have heavy write/append actions on the same text file, I can envisage problems. A db table would probably be better as appends could be just inserted. In addition, it's easier to edit or delete discrete sections.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.