...

View Full Version : Reading created files and transferring them to select list



Jutlander
08-03-2007, 11:43 AM
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
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($file, 0755); //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_file, file_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.



<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.



<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.

devinemke
08-03-2007, 04:55 PM
if you are using PHP5: scandir (http://www.php.net/scandir)
if you are using PHP4: opendir (http://www.php.net/opendir) readdir (http://www.php.net/readdir)

Jutlander
08-03-2007, 05:33 PM
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:



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

devinemke
08-03-2007, 05:37 PM
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

Jutlander
08-03-2007, 05:45 PM
OK, thanks for clearing that up. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum