tronicsmasta Posted May 4, 2008 Share Posted May 4, 2008 Hey guys, I usually don't request help with php unless I am scratching my head like tonight lol. I am using a copy of Max's Guestbook... All worked ok when the files were all in the same directory as the .html file that called the scripts. I have attempted, yes keyword attempted, to modify the script to use a different directory on the server... no luck... Directory Structure I am attempting to use: /channel.html /video/1_channel/ /video/1_channel/comments.class.php /video/1_channel/comments/ (chmodded to 777) /video/1_channel/comments/05032008195332_IP(hidden).txt (example comment in the directory) When channel.html, comments.class.php, and comments/ where in the same directory no bubbles no troubles... When it follows the directory structure above... no go... I tried assuming that the script would call for folders/files in its directory because the script runs under its own dir... eg. (comments.class.php would call files under the comments/ folder when referenced to its own directory...) However this is not the case... I tried changing the commentsDir to /video/1_channel/comments and still no go... idk why? Anyhow, if you experts can assist me that would be great. My code is below. Embeded code into channel.html: <?php require_once("video/1_channel/comments.class.php"); $myComments = new maxComments(); $myComments->processComments(); ?> comments.class.php code: <?php class maxComments{ var $commentsDir = 'comments'; var $dateFormat = 'm.d.Y g:i A'; var $itemsPerPage = 5; var $commentsList; function processComments(){ if (isset($_POST['submit'])) { $this->insertComments(); } $page = isset($_GET['page']) ? $_GET['page'] : 1; $this->displayComments($page); } function getCommentsList(){ $this->commentsList = array(); // Open the actual directory if ($handle = @opendir($this->commentsDir)) { // Read all file from the actual directory while ($file = readdir($handle)) { if (!is_dir($file)) { $this->commentsList[] = $file; } } } rsort($this->commentsList); return $this->commentsList; } function displayComments($page=1){ $list = $this->getCommentsList(); //echo "<center><a href='add.php'>Leave a comment</a></center>"; echo "<div class='comments'>"; //Get start point and end point $startItem = ($page-1)*$this->itemsPerPage; if (($startItem + $this->itemsPerPage) > sizeof($list)) $endItem = sizeof($list); else $endItem = $startItem + $this->itemsPerPage; for ($i=$startItem;$i<$endItem;$i++){ //foreach ($list as $value) { $value = $list[$i]; $data = file($this->commentsDir.DIRECTORY_SEPARATOR.$value); $name = trim($data[0]); $email = trim($data[1]); $submitDate = trim($data[2]); unset ($data['0']); unset ($data['1']); unset ($data['2']); $content = ""; foreach ($data as $value) { $content .= $value; } echo "<ul><li><h3>$submitDate by <span id=\"name\">$name</span></h3>"; echo " ".nl2br(htmlspecialchars($content))."<br/></li></ul>"; } echo "</div>"; if (sizeof($list) == 0){ echo "<div id=\"comments\"><ul><li><h3>No comments at the moment!</h3></li></ul></div>"; } // Create pagination if (sizeof($list) > $this->itemsPerPage){ echo "<div id=\"commentNav\">"; if ($startItem == 0) { if ($endItem < sizeof($list)){ echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next »</a></div>"; } else { // Nothing to display } } else { if ($endItem < sizeof($list)){ echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >« Prev</a></div>"; echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next »</a></div>"; } else { echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >« Prev</a></div>"; } } echo "<br/></div><br/>"; } echo "<hr />"; $this->displayAddForm(); } function displayAddForm(){ ?> <div id="form"> <form class="iform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Name:<br/> <input type="text" name="name" size="30"/><br/><br/> Comment:<br/> <textarea name="comments" rows="7" cols="49"></textarea><br/> <input type="submit" name="submit" value="Leave Comment" /> </form> </div> <?php } function insertComments(){ $name = isset($_POST['name']) ? $_POST['name'] : 'Anonymous'; $email = isset($_POST['email']) ? $_POST['email'] : ''; $ip = $_SERVER['REMOTE_ADDR']; $submitDate = date($this->dateFormat); $content = isset($_POST['comments']) ? $_POST['comments'] : ''; if (trim($name) == '') $name = 'Anonymous'; if (strlen($content)<5) { exit(); } $filename = date('mdYHis')."_$ip"; if (!file_exists($this->commentsDir)){ mkdir($this->commentsDir); } $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+"); fwrite($f,$name."\n"); fwrite($f,$email."\n"); fwrite($f,$submitDate."\n"); fwrite($f,$content."\n"); fclose($f); } } ?> Even when i put a manual comment in the comments directory, it says no comments... when i add a comment the script fails and stops the rest of the html from processing... any ideas? Thank you!!! Quinton Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 IDK what those random boxes are lol.... Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 Does anyone have the foggiest idea? thanks, Quinton Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Look in the comments class file. Change (it's near the first line): var $commentsDir = 'comments'; To: $commentsDir = 'video/1_channel/comments'; You don't need the var declaration. This will work if it's going to be static. Otherwise, you'll need to do some changing to the class. But if comments are going to ONLY be in that directory, that should work. =) Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 i am new to classes and am not sure about some of the code. I figured out I was tired and uploading the wrong copy of the file... so now i read the comments in the comments folder, but when I click add comment to submit my comment... the php script errors and i get nothing from that part of the page down... I tried this class maxComments{ var $commentsDir = 'video/1_channel/comments'; var $dateFormat = 'm.d.Y g:i A'; var $itemsPerPage = 5; var $commentsList; and also tried $commentsDir = 'video/1_channel/comments'; class maxComments{ var $dateFormat = 'm.d.Y g:i A'; var $itemsPerPage = 5; var $commentsList; and no go... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Use the first code block you posted (the second one won't do a thing), and please show us the error... Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 i tired both... when i click submit on the form, it doesn't process... no errors just a blank section of that page... i have error reporting on and still no errors or warnings. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Change: function insertComments(){ $name = isset($_POST['name']) ? $_POST['name'] : 'Anonymous'; $email = isset($_POST['email']) ? $_POST['email'] : ''; $ip = $_SERVER['REMOTE_ADDR']; $submitDate = date($this->dateFormat); $content = isset($_POST['comments']) ? $_POST['comments'] : ''; if (trim($name) == '') $name = 'Anonymous'; if (strlen($content)<5) { exit(); } $filename = date('mdYHis')."_$ip"; if (!file_exists($this->commentsDir)){ mkdir($this->commentsDir); } $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+"); fwrite($f,$name."\n"); fwrite($f,$email."\n"); fwrite($f,$submitDate."\n"); fwrite($f,$content."\n"); fclose($f); } To: function insertComments(){ $name = isset($_POST['name']) ? $_POST['name'] : 'Anonymous'; $email = isset($_POST['email']) ? $_POST['email'] : ''; $ip = $_SERVER['REMOTE_ADDR']; $submitDate = date($this->dateFormat); $content = isset($_POST['comments']) ? $_POST['comments'] : ''; if (trim($name) == '') $name = 'Anonymous'; if (strlen($content)<5) { exit(); } $filename = date('mdYHis')."_$ip"; if (!file_exists($this->commentsDir)){ mkdir($this->commentsDir); } $f = fopen($this->commentsDir.DIRECTORY_SEPARATOR.$filename.".txt","w+"); fwrite($f,$name."\n"); fwrite($f,$email."\n"); fwrite($f,$submitDate."\n"); fwrite($f,$content."\n"); fclose($f); echo "Comment added successfully"; } Now, if it succeeds, it'll show something. And you can actually VIEW the comments now? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 I just looked at your site from the PM...and you gave me the wrong directory in the beginning... Change the commentsDir thing to this: $commentsDir = "video/1_hollywoodnow/comments"; >_> Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 the comments do show... look at the link i pm'ed you... but, when I submit the form below, i get nothing at all when it works, you click submit and after the refresh, the comment shows right away. basically it stops processing now... eg from view source of the page... <!-- comments --> <div id="comments"> <h2>Comments</h2> the PHP output is usually here! Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 i was trying to keep that info out of the post but yes that is what i was using... i have updated it since you have viewed the page... Q Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 The comment adds for me. Look at it. And you must have an extra blank .txt in your comments directory, so delete it, and it should be all good. =) Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 i saw your comment... i deleted all the files in the dir. i have a random blank one but there is nothing in the comments directory... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 This is why I hate flatfile storage. I'm not even kidding when I say this, but I think you should rewrite your own guestbook to use a MySQL database. So much more efficient and easier to work with. =/ Quote Link to comment Share on other sites More sharing options...
tronicsmasta Posted May 4, 2008 Author Share Posted May 4, 2008 agreed... i will re-write it... i was going to orginally do that but i found this and it was easy to modify. thanks for your help! Q Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 4, 2008 Share Posted May 4, 2008 Any time. @_@ If you need help, feel free to post back and ask. 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.