Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Jutlander's Avatar
    Join Date
    Jun 2007
    Location
    In my own sick little world :P
    Posts
    425
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Reading created files and transferring them to select list

    I'll be brief here. The below script creates a new file, with a .php extension, to the same directory on submit. It redirects to the admin page when the page is created and the user may select the files he has created from the <select> list. Problem is, I don't know how to do this.

    createfile.php
    PHP Code:
    <?php
    if(isset($_POST['submit'])) //has the form been submitted?
    {
        
    $file_name strip_tags($_POST['file_name']);//the file's name, stripped of any dangerous tags
        
    $file_ext strip_tags($_POST['file_ext']); //the file's extension, stripped of any dangerous tags
        
    $file $file_name.$file_ext//this is the entire filename
        
    $create_file fopen($file'w'); //create the new file
        
    if(!$create_file)
    {
        die(
    "There was an error creating/opening the file! Make sure you have the correct permissions!\n");
    }
    $chmod chmod($file0755); //set the appropriate permissions.

    if(!$chmod//attempt to set the permissions
    {
        echo(
    "There was an error changing the permissions of the new file!\n"); //error changing the file's permissions
    }
    if (
    fwrite($create_filefile_get_contents("template.php")) === FALSE//attempt to write basic content to the file
    {
        echo 
    "Error writing to file: ($file)\n";
    }
    fclose($create_file);
        
    header("Location: admin.php"); //tell the user that the file has been created successfully
        
    exit; //exit the script
    }
    else 
    //the form hasn't been submitted!
    {
        
    header("Location: createfile.php"); //redirect the user back to the add file page
        
    exit; //exit the script
    }
    ?>
    Below is the form for creating files, in case you need it.

    Code:
    <form action="createfile.php" method="post">
    <div><label>File name:</label>&nbsp;<input type="text" name="file_name"><br><br>
    <label>File Extension:</label>
    <select name="file_ext">
    <option value=".php">.php</option>
    </select><br><br>
    <input type="submit" class="button" name="submit" value="Create file"></div>
    </form>
    Below is the form for editing the created files.

    Code:
    <form action="editfile.php" method="post">
    <div><label>Page to edit:</label>
    <select name="edit_file">
    <option></option>
    <option></option>
    <option></option>
    <option></option>
    </select><br><br>
    <input type="submit" class="button" name="editsubmit" value="Edit page"></div>
    </form>
    Obviously I want the names of the created pages to list as option values. These two forms are on the same page, the admin page.
    Last edited by Jutlander; 08-03-2007 at 12:18 PM. Reason: forgot to end form tags
    .
    .

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    if you are using PHP5: scandir
    if you are using PHP4: opendir readdir

  • #3
    Regular Coder Jutlander's Avatar
    Join Date
    Jun 2007
    Location
    In my own sick little world :P
    Posts
    425
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Thanks for your reply, I didn't get along very well with scandir() though. OK, after spending a bit of time trying to work this out, I came up with this and it seems to work:

    Code:
    	<form action="editfile.php" method="post">
    	<div><label>Page to edit:</label>
    	<select name="edit_files">
    <?php
    if ($handle = opendir('./')) {
        echo "Files:<br>";
        while (false !== ($file = readdir($handle))) {
            echo "<option>$file</option>";
        }
    closedir($handle);
    }
    ?>
    	</select><br><br>
    	<input type="submit" class="button" name="edit-submit" value="Edit page"></div>
    	</form>
    Problem now is that each option must have a different value to seperate them from eachother?
    .
    .

  • #4
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    Quote Originally Posted by Jutlander View Post
    Problem now is that each option must have a different value to seperate them from eachother?
    each filename is unique thus each value will be unique

  • #5
    Regular Coder Jutlander's Avatar
    Join Date
    Jun 2007
    Location
    In my own sick little world :P
    Posts
    425
    Thanks
    1
    Thanked 12 Times in 12 Posts
    OK, thanks for clearing that up.
    .
    .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •