Updating files problem.

  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 7th, 2008, 8:50 pm

I'm using the following file thing to update files... the only problem with it is that it doesn't save.

I'm using the following code.
  1. <form action="files.php" method="post">
  2. Select file to edit
  3. <?php
  4. echo "<select name=\"file\">\n";
  5. foreach (new DirectoryIterator('.') as $file) {
  6.    // if the file is not this file, and does not start with a '.' or '..',
  7.    // then store it for later display
  8.    if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
  9.       $addr = ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename();
  10.       echo "<option value=\"$addr\">";
  11.       // if the element is a directory add to the file name "(Dir)"
  12.       echo $addr;
  13.       echo "</option>\n";
  14.    }
  15. }
  16. echo "</select>\n";
  17. ?>
  18. <input type="submit" value="submit" name="submit" />
  19. </form>
  20. <?php
  21. if(isset($_POST['submit']))
  22. {
  23.   $filename = $_POST['file'];
  24.  
  25.   if($fhandle = fopen($filename, "r+"))
  26.   {
  27.     $contents = file_get_contents($filename);
  28.     echo "Editing <strong>$filename</strong><br />\n";
  29.     echo "<form action=\"files.php\" method=\"post\">";
  30.     echo "<textarea rows=\"30\" cols=\"100\" name=\"contents2\">$contents</textarea><br />\n";
  31.     echo "<input type=\"submit\" value=\"Save\" name=\"save\" /> <input type=\"reset\" value=\"Reset\" name=\"reset\" />";
  32.     echo "</form>\n";
  33.     if(isset($_POST['save']))
  34.     {
  35.       echo = "Hi\n";
  36.       $contents2 = $_POST['contents2'];
  37.       if(file_put_contents($filename, $contents2, "LOCK_EX"))
  38.       {
  39.         echo '<p>File successfully edited</p>'."\n";
  40.       } else {
  41.         echo '<p>Could not write the contents to file.</p>'."\n";
  42.         echo "<textarea rows=\"30\" cols=\"100\">$contents2</textarea><br />\n";
  43.       }
  44.     }
  45.     fclose($fhandle);
  46.   } else {
  47.     echo '<p>Failed to open file. Make sure that the file is of correct type. It cannot be a directory.</p>'."\n";
  48.   }
  49. }
  50. ?>

I put in the "Hi" there... it doesn't print so the "Save" button is not submitting... any reason behind that?

Thanks for any help... It doesn't give me any errors...
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 7th, 2008, 8:50 pm

  • spork
  • Born
  • Silver Member
  • User avatar
  • Joined: 22 Sep 2003
  • Posts: 3828
  • Loc: Rochester, NY
  • Status: Offline

Post May 8th, 2008, 9:48 am

  1. echo = "Hi\n";


I'm surprised you don't get a parsing error.
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 8th, 2008, 11:41 am

I don't and know that you singled that thing out I see the problem :lol:

But it still doesn't save anything into the file.

[EDIT] Also, I don't know why the echo = thing is even here... I just now checked the file and there is no such thing... there is only echo 'Hi'; and thats it...
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • spork
  • Born
  • Silver Member
  • User avatar
  • Joined: 22 Sep 2003
  • Posts: 3828
  • Loc: Rochester, NY
  • Status: Offline
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 8th, 2008, 8:09 pm

God I feel stupid. I found the problem. For the whole validation, I'm checking if a value "submit" has being set, but on my other form inside that check has the name of "Save" rather than submit... A simple hidden field by the name of "submit" fixed the problem. Thanks though spork.

Another problem now...

It doesn't save when I submit the content to be saved. I get the made up error...
Quote:
Failed to open file. Make sure that the file is of correct type. It cannot be a directory.

But no actual parsing error or anything... the updated code now is...
  1. <form action="files.php" method="post">
  2. Select file to edit
  3. <?php
  4. echo "<select name=\"file\">\n";
  5. foreach (new DirectoryIterator('.') as $file) {
  6.    // if the file is not this file, and does not start with a '.' or '..',
  7.    // then store it for later display
  8.    if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
  9.       $addr = ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename();
  10.       echo "<option value=\"$addr\">";
  11.       // if the element is a directory add to the file name "(Dir)"
  12.       echo $addr;
  13.       echo "</option>\n";
  14.    }
  15. }
  16. echo "</select>\n";
  17. echo '<input type="submit" value="Submit" name="submit" />';
  18. echo '</form>';
  19. if(isset($_POST['submit']))
  20. {
  21.   $filename = $_POST['file'];
  22.   $fhandle = fopen($filename, "r+");
  23.   if($fhandle)
  24.   {
  25.     $contents = file_get_contents($filename);
  26.     echo 'Editing <strong>'. $filename .'</strong><br />'."\n";
  27.     echo '<form action="files.php" method="post">';
  28.     echo '<textarea rows="30" cols="100" name="contents2">'. $contents .'</textarea><br />'."\n";
  29.     echo '<input type="hidden" value="submit" name="submit" />'."\n";
  30.     echo '<input type="hidden" value="'. $filename .'" name="file" />'."\n";
  31.     echo '<input type="submit" value="Save" name="Save" /> <input type="reset" value="Reset" name="reset" />';
  32.     echo '</form>'."\n";
  33.     if(isset($_POST['Save']))
  34.     {
  35.       echo 'Hi';
  36.       $filename = $_POST['file'];
  37.       $contents2 = $_POST['contents2'];
  38.       $write = file_put_contents($filename, $contents2);
  39.       if($write)
  40.       {
  41.         echo '<p>File successfully edited</p>'."\n";
  42.       } else {
  43.         echo '<p>Could not write the contents to file.</p>'."\n";
  44.         echo '<textarea rows="30" cols="100">'. $contents2 .'</textarea><br />'."\n";
  45.       }
  46.     }
  47.     fclose($fhandle);
  48.   } else {
  49.     echo '<p>Failed to open file. Make sure that the file is of correct type. It cannot be a directory.</p>'."\n";
  50.   }
  51. }
  52. ?>

Any help would be great... Reopening the file in the place before replacing the data doesn't help...

[EDIT] Well, turns out that the first time I set the variable $filename it got cleared after my second submittal (The save) so I had to add another hidden field with the name of the file and then reset the variable... the code above is changed to the working one :D The added in part is in orange :)
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 9th, 2008, 8:44 pm

Alright. I completely redid the whole thing right now :lol: Here is the code for it right now...
  1. <?php
  2. foreach(new DirectoryIterator('.') as $file)
  3. {
  4.   if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
  5.     if($file->isDir())
  6.     {
  7.       echo '[DIR] <a href="files.php?dir='. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  8.     } else {
  9.       echo '<a href="files.php?file='. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  10.     }
  11.   }
  12. }
  13. if(isset($_GET['file']) && !isset($_GET['dir']))
  14. {
  15.   $filename = $_GET['file'];
  16.   $fhandle = fopen($filename, "r+");
  17.   if($fhandle && ($filename != basename($_SERVER['PHP_SELF'])))
  18.   {
  19.     $contents = file_get_contents($filename);
  20.     echo 'Editing <strong>'. $filename .'</strong><br />'."\n";
  21.     echo '<form action="files.php?file='. $filename .'" method="post">';
  22.     echo '<textarea rows="30" cols="100" name="contents2">'. $contents .'</textarea><br />'."\n";
  23.     echo '<input type="hidden" value="'. $filename .'" name="file" />'."\n";
  24.     echo '<input type="submit" value="Save" name="Save" /> <input type="reset" value="Reset" name="reset" />';
  25.     echo '</form>'."\n";
  26.     if(isset($_POST['Save']))
  27.     {
  28.       $contents2 = $_POST['contents2'];
  29.       $filename = $_POST['file'];
  30.       $write = file_put_contents($filename, $contents2);
  31.       if($write)
  32.       {
  33.         echo '<p>File successfully edited</p>'."\n";
  34.       } else {
  35.         echo '<p>Could not write the contents to file.</p>'."\n";
  36.         echo '<textarea rows="30" cols="100">'. $contents2 .'</textarea><br />'."\n";
  37.       }
  38.     }
  39.     fclose($fhandle);
  40.   } else {
  41.     echo '<p>Failed to open file. The problem could be because:</p>'."\n";
  42.     echo '<ol>'."\n";
  43.     echo '<li>You are trying to edit this file</li>'."\n";
  44.     echo '<li>The file is a directory</li>'."\n";
  45.     echo '<li>The file doesn\'t exist</li>'."\n";
  46.     echo '</ol>'."\n";
  47.   }
  48. }
  49. if(isset($_GET['dir']) && !isset($_GET['file']))
  50. {
  51.  
  52.  
  53. }
  54. ?>

The only problem with it is I can't find a way to make it so I can open the directory... Any help on it?

[EDIT] The following code seemed to fix it
  1. <?php
  2. if(isset($_GET['dir']) && !isset($_GET['file']))
  3. {
  4.   $dir = $_GET['dir'];
  5.   foreach(new DirectoryIterator('../'. $dir) as $file)
  6.   {
  7.     if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
  8.       if($file->isDir())
  9.       {
  10.         echo '[DIR] <a href="files.php?dir='. $dir .'/'. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  11.       } else {
  12.         echo '<a href="files.php?file='. $dir .'/'. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  13.       }
  14.     }
  15.   }
  16. } else {
  17.   foreach(new DirectoryIterator('..') as $file)
  18.   {
  19.     if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
  20.       if($file->isDir())
  21.       {
  22.         echo '[DIR] <a href="files.php?dir='. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  23.       } else {
  24.         echo '<a href="files.php?file='. $file->getFilename() .'">'. $file->getFilename() .'</a><br />'."\n";
  25.       }
  26.     }
  27.   }
  28. }
  29. if(isset($_GET['file']) && !isset($_GET['dir']))
  30. {
  31.   $filename = $_GET['file'];
  32.   $fhandle = fopen('../'. $filename, "r+");
  33.   if($fhandle && ('../'. $filename != basename($_SERVER['PHP_SELF'])))
  34.   {
  35.     $contents = file_get_contents('../'. $filename);
  36.     echo 'Editing <strong>'. $filename .'</strong><br />'."\n";
  37.     echo '<form action="files.php?file='. $filename .'" method="post">';
  38.     echo '<div>'."\n";
  39.     echo '<textarea rows="30" cols="71.5" name="contents2">'. $contents .'</textarea><br />'."\n";
  40.     echo '<input type="hidden" value="'. $filename .'" name="file" />'."\n";
  41.     echo '<input type="submit" value="Save" name="Save" /> <input type="reset" value="Reset" name="reset" />';
  42.     echo '</div>'."\n";
  43.     echo '</form>'."\n";
  44.     if(isset($_POST['Save']))
  45.     {
  46.       $contents2 = $_POST['contents2'];
  47.       $filename = $_POST['file'];
  48.       $write = file_put_contents('../'. $filename, $contents2);
  49.       if($write)
  50.       {
  51.         echo '<p>File successfully edited</p>'."\n";
  52.       } else {
  53.         echo '<p>Could not write the contents to file.</p>'."\n";
  54.         echo '<textarea rows="30" cols="100">'. $contents2 .'</textarea><br />'."\n";
  55.       }
  56.     }
  57.     fclose($fhandle);
  58.   } else {
  59.     echo '<p>Failed to open file. The problem could be because:</p>'."\n";
  60.     echo '<ol>'."\n";
  61.     echo '<li>You are trying to edit this file</li>'."\n";
  62.     echo '<li>The file is a directory</li>'."\n";
  63.     echo '<li>The file doesn\'t exist</li>'."\n";
  64.     echo '</ol>'."\n";
  65.   }
  66. }
  67. ?>
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 11th, 2008, 8:57 pm

How can I make it so when I click on a link it takes me to one directory back?
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • spork
  • Born
  • Silver Member
  • User avatar
  • Joined: 22 Sep 2003
  • Posts: 3828
  • Loc: Rochester, NY
  • Status: Offline
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 12th, 2008, 2:19 pm

spork wrote:
../

You don't understand... how can I do this using the script I put above... The script looks like this right now...
  1. <?php
  2.     $thisPage = basename($_SERVER['PHP_SELF']);
  3.     echo '<div class="FileEditMenu">'."\n";
  4.     echo '<p>Select which file to edit</p>'."\n";
  5.     if(isset($_GET['dir']))
  6.     {
  7.       $dir = $_GET['dir'];
  8.       echo '<a href="'. $thisPage .'?dir=../">Go Back</a>'."\n"; //<-- Very funny LOL
  9.       foreach(new DirectoryIterator('../'. $dir) as $file)
  10.       {
  11.         if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
  12.           if($file->isDir())
  13.           {
  14.             echo '[DIR] <a href="'. $thisPage .'?dir='. $dir .'/'. $file->getFilename() .'&amp;dir='. $dir .'&amp;act=EditFiles">'. $file->getFilename() .'</a><br />'."\n";
  15.           } else {
  16.             echo '<a href="'. $thisPage .'?file='. $dir .'/'. $file->getFilename() .'&amp;dir='. $dir .'&amp;act=EditFiles">'. $file->getFilename() .'</a><br />'."\n";
  17.           }
  18.         }
  19.       }
  20.     } else {
  21.       foreach(new DirectoryIterator('..') as $file)
  22.       {
  23.         if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF']))) {
  24.           if($file->isDir())
  25.           {
  26.             echo '[DIR] <a href="'. $thisPage .'?dir='. $file->getFilename() .'&amp;act=EditFiles">'. $file->getFilename() .'</a><br />'."\n";
  27.           } else {
  28.             echo '<a href="'. $thisPage .'?file='. $file->getFilename() .'&amp;act=EditFiles">'. $file->getFilename() .'</a><br />'."\n";
  29.           }
  30.         }
  31.       }
  32.     }
  33.     echo '<p style="font-size: 12px;">[DIR] = Directory</p>'."\n";
  34.     echo '</div>'."\n";
  35.    
  36.     $dir = '&amp;dir='. $_GET['dir'];
  37.     if(isset($_GET['file']))
  38.     {
  39.       $filename = $_GET['file'];
  40.       $fhandle = fopen('../'. $filename, "r+");
  41.       if($fhandle && ('../'. $filename != basename($_SERVER['PHP_SELF'])))
  42.       {
  43.         $contents = file_get_contents('../'. $filename);
  44.         echo 'Editing <strong>'. $filename .'</strong><br />'."\n";
  45.         echo '<form action="'. $thisPage .'?file='. $filename . $dir .'&amp;act=EditFiles" method="post">';
  46.         echo '<div>'."\n";
  47.         $contents = htmlspecialchars($contents);
  48.         echo '<textarea rows="36" cols="87.5" name="contents2">'. $contents .'</textarea><br />'."\n";
  49.         echo '<input type="hidden" value="'. $filename .'" name="file" />'."\n";
  50.         echo '<input type="submit" value="Save" name="Save" /> <input type="reset" value="Reset" name="reset" />';
  51.         echo '</div>'."\n";
  52.         echo '</form>'."\n";
  53.         if(isset($_POST['Save']))
  54.         {
  55.           $contents2 = $_POST['contents2'];
  56.           $filename = $_POST['file'];
  57.           $write = file_put_contents('../'. $filename, $contents2);
  58.           if($write)
  59.           {
  60.             echo '<div class="msg">'."\n";
  61.             echo '<p>File successfully edited</p>'."\n";
  62.             echo '</div>'."\n";
  63.           } else {
  64.             echo '<div class="msg">'."\n";
  65.             echo '<p>Could not write the contents to file.</p>'."\n";
  66.             echo '<textarea rows="30" cols="100">'. $contents2 .'</textarea><br />'."\n";
  67.             echo '</div>'."\n";
  68.           }
  69.         }
  70.         fclose($fhandle);
  71.       } else {
  72.         echo '<div class="msg">'."\n";
  73.             echo '<p>Failed to open file. The problem could be because:</p>'."\n";
  74.         echo '<ol>'."\n";
  75.         echo '<li>You are trying to edit this file</li>'."\n";
  76.         echo '<li>The file is a directory</li>'."\n";
  77.         echo '<li>The file doesn\'t exist</li>'."\n";
  78.         echo '</ol>'."\n";
  79.             echo '</div>'."\n";
  80.       }
  81.     }
  82. ?>

When I click on a directory... let's say I'm in the directory /files/ right now and I go to site/ in that directory the link would be files.php?dir=../files/site and then when I go to more and more directories it would look... files.php?dir=../files/site/images/forum/contacts/friends

(Sorry for the messy code... I use NotePad++ and it "auto tabs"... not a problem, but if it used regular spaces and not tabs (\r)...
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so
  • spork
  • Born
  • Silver Member
  • User avatar
  • Joined: 22 Sep 2003
  • Posts: 3828
  • Loc: Rochester, NY
  • Status: Offline

Post May 12th, 2008, 5:14 pm

Keep in mind that ".." can go anywhere in the URL, not just at the beginning, and it can appear as many times as you want.
  • Bogey
  • Ounce of 'Zu'
  • Web Master
  • User avatar
  • Joined: 14 Jul 2005
  • Posts: 4671
  • Loc: Ozzu
  • Status: Offline

Post May 12th, 2008, 5:54 pm

spork wrote:
Keep in mind that ".." can go anywhere in the URL, not just at the beginning, and it can appear as many times as you want.

Yeah, I know... but the dir thing changes dynamically... and if the url looks like files.php?dir=../files/site/images/forum/contacts/friends than the link to go back 1 directory would be files.php?dir=../files/site/images/forum/contacts -- Notice that the friends directory was removed... I know I would need a preg_match thing... how can I do that?

Basically, I want a preg_match_all thing that removes the last one... I tried the following...
  1. preg_match_all('$.*[\/]*.?.*[\/]*.$',$dir,$stick);

But the array looks like...
Quote:
Array ( [0] => files.php?dir=../files/site/images/forum/contacts/friends )

Exactly the same... what I wanted to look for is for all matches that are between the /'s since the last one isn't between I thought it would keep that thing out :lol: I guess not... maybe there's a better and correct way to do this?

[EDIT] I am now using the following regex
  1. preg_match_all('$.*[\/]+[\/]*.$',$dir,$stick);

But now it gives me the following...
ORIGINAL: files.php?dir=../files/site/images/forum/contacts/friends
|EDITED : files.php?dir=../files/site/images/forum/contacts/f
And no matter how the directory looks like it always puts that / in there and the first letter of the next directory... Any help?

[EDIT 2] I now use the following regex
  1. preg_match_all('$.*[\/]+[\/]*$',$tt,$stick);

And now it gives me following results
ORIGINAL: files.php?dir=../files/site/images/forum/contacts/friends
|EDITED : files.php?dir=../files/site/images/forum/contacts/
I know that it works how it should, but is there any way to remove that last "/"?

[EDIT 3] Now I'm trying to preg_replace the match that doesn't have a "/" at the end end remove "/directory"... Can't seem to make it to work... I'm not using any of the above script... Any help?

[EDIT 4] AWESOME! I fixed the problem! I used the following regex
  1. preg_match_all('$.*[\/]+[\/]*$',$dir,$stick)

I implemented that on my code and got the Go Back Feature :D

Here is how the code looks like now...
  1. <?php
  2.       $dir = $_GET['dir'];
  3.       if(preg_match_all('$.*[\/]+[\/]*$',$dir,$stick))
  4.       {
  5.       $cnt2 = strlen($stick[0][0]);
  6.       foreach($stick[0] as $value)
  7.       {
  8.         echo '<p><a href="'. $thisPage .'?dir='. substr($value, 0, $cnt2-1) .'&amp;act=EditFiles">Go Back</a></p>'."\n";
  9.       }
  10.       } else {
  11.         echo '<p><a href="'. $thisPage .'?act=EditFiles">Go Back</a></p>'."\n";
  12.       }
  13. ?>

Thanks though :) So for now all of my File Update System problems are over :)
My Developing Blog (7)
Wedevoy.com - In Development... should be done in about a week or so

Post Information

  • Total Posts in this topic: 11 posts
  • Moderators: joebert, katana
  • Users browsing this forum: No registered users and 75 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2008. Driven by phpBB © 2001-2008 phpBB Group.

 
 
 
 

Need a pre-made web design for your website?

Check out our templates here: Ozzu Templates


400+ FREE Website Templates. Download Now!