Jump to content

Php file editing


seularts

Recommended Posts

I have the following code:

 

<?php

$path = "dir/"; //change this if the script is in a different dir that the files you want

$show = array( '.jpg', '.gif', '.txt' ); //Type of files to show

 

$select = "<select name=\"select_box\">";

 

$dh = @opendir( $path );

 

while( false !== ( $file = readdir( $dh ) ) ){

    $ext=substr($file,-4,4);

        if(in_array( $ext, $show )){     

            $select .= "<option value='$path/$file'>$file</option>\n";

    }

 

$select .= "</select>";

 

closedir( $dh );

 

echo "$select";

 

if($_POST['edit']) {

$filename = $_POST['file'];

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

echo "<form method=\"post\" action=\"index.php?area=edit\">

<strong>$filename</strong><br>

<input type=\"hidden\" name=\"file\" value=\"$filename\">

<textarea name=\"content\" cols=\"60\" rows=\"20\">".$contents."</textarea><br>

<input type=\"submit\" name=\"update\" value=\"Update\">

</form>";

fclose($handle);

} elseif($_POST['update']) {

$filename = $_POST['file'];

if(is_writable($filename)) {

$handle = fopen($filename, "w+");

fwrite($handle, $_POST['content']);

fclose($handle);

echo "File: <strong>". $filename . "</strong> edited successfully.<br><a href=\"$PHP_SELF\">Edit More Files</a>";

} else {

echo "Error! <strong>". $filename . "</strong> File may not be writable.";

}

} else {

echo "<form method=\"post\" action=\"$PHP_SELF\">

File: <input type=\"text\" name=\"file\" value=$file><br>

<input type=\"submit\" name=\"edit\" value=\"Edit\">

</form>";

}

?>

 

the thing is that it works if i type the file by hand.. but i want to get the file from the drom menu .. I have no ideea how to get the 2 pieces of code to go together.. Can anyone help!?

Link to comment
https://forums.phpfreaks.com/topic/105417-php-file-editing/
Share on other sites

I want to use the drop down menu to edit a specific file from the folder from where it reads.. instead of writing by hand in the input field the name of the file from that folder! That is why I wrote boath codes together.. Mainly I want to get ridd of the input field.. but i have no ideea how to use the same function for the drop down menu!

Link to comment
https://forums.phpfreaks.com/topic/105417-php-file-editing/#findComment-539882
Share on other sites

Ok i got to this point with my script:

 

<?php

$path = "dir/";

$show = array( '.jpg', '.gif', '.txt' ); //Type of files to show

 

$select = "<select name=\"file\">";

 

$dh = @opendir( $path );

 

while( false !== ( $file = readdir( $dh ) ) ){

    $ext=substr($file,-4,4);

        if(in_array( $ext, $show )){     

            $select .= "<option value='$path/$file'>$file</option>\n";

    }

$aaa=$select;

 

$select .= "</select>";

 

closedir( $dh );

 

if($_POST['edit']) {

$filenamex = $_POST['file'];

$boom = explode("//", $filenamex);

$lolExplosion = explode(".", end($boom));

$filename=$lolExplosion[0].'.txt';

$handle = fopen($path.$filename, "r");

$contents = fread($handle, filesize($filename));

echo "<form method=\"post\" action=\"index.php?area=edit\">

<strong>$filename</strong><br>

<input type=\"hidden\" name=\"file\" value=\"$filename\">

<textarea name=\"content\" cols=\"60\" rows=\"20\">".$contents."</textarea><br>

<input type=\"submit\" name=\"update\" value=\"Update\">

</form>";

fclose($handle);

} elseif($_POST['update']) {

$filename = $_POST['file'];

if(is_writable($filename)) {

$handle = fopen($filename, "w+");

fwrite($handle, $_POST['content']);

fclose($handle);

echo "File: <strong>". $filename . "</strong> edited successfully.<br><a href=\"$PHP_SELF\">Edit More Files</a>";

} else {

echo "Error! <strong>". $filename . "</strong> File may not be writable.";

}

} else {

/*File: <input type=\"text\" name=\"file\" value=$file><br>"*/

echo "<form method=\"post\" action=\"$PHP_SELF\">

$aaa<input type=\"submit\" name=\"edit\" value=\"Edit\">

</form>";

}

?>

 

It reads the document from the path i specify, but when i try to open the file through php i het the followinf error:

 

Warning: filesize() [function.filesize]: stat failed for Copy of a.txt in D:\xampp\htdocs\tzr\wr\edit\index.php on line 28

 

Warning: fread() [function.fread]: Length parameter must be greater than 0 in D:\xampp\htdocs\tzr\wr\edit\index.php on line 28

Link to comment
https://forums.phpfreaks.com/topic/105417-php-file-editing/#findComment-539976
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.