A
Anonymous
Guest
hi i want to be able to load a file into a txt box, edit the content of the file and then save it. can anyone help me?
Many thanks
Many thanks
<?php
if ($path == null)
$path=".";
if ($file == "undefined")
$file = null;
$path = urldecode($path);
$file = urldecode($file);
?>
<html>
<head>
<META HTTP-EQUIV="Expires" CONTENT="-1">
<title>File Browser <?php if ($file == null) echo $path; else echo $file; ?></title>
</head>
<body>
<?php
function displaydir()
{
global $path, $pw;
$dir = opendir ($path);
while ($file = readdir($dir)):
if (is_dir($file)):
if ($file == "."):
echo ('<b><A HREF="javascript:go(\''. urlencode($path) .'\');">'.$file.'/</a></b><br>');
elseif ($file == ".."):
echo ('<b><A HREF="javascript:go(\'' . urlencode(dirname($path)) .'\');">'.$file.'/</a></b><br>');
else:
echo ('<b><A HREF="javascript:go(\'' . urlencode($path . '/'. $file) .'\');">'.$file.'/</a></b><br>');
endif;
else:
echo ('<A HREF="javascript:go(\'' . urlencode($path) . '\',\''. urlencode($path . '/'. $file) .'\');">'.$file.'</a><br>');
endif;
endwhile;
closedir($dir);
echo ('<input type="radio" name="action" value="view" checked>View<input type="radio" name="action" value="edit">Edit<input type="radio" name="action" value="delete">Delete<input type="radio" name="action" value="rename">Rename<input type="radio" name="action" value="copy">Copy<br>');
echo ('<input type="text" name="text1"><input type="submit" name="new" value="New File"><input type="submit" name="new" value="New Dir"><br>');
echo ('Upload:<br>');
echo ('<input type="file" name="userfile">');
echo ('<input type="submit" name="action" value="upload">');
}
function displayfile()
{
global $file, $action, $area, $path,$pw;
echo ('<b><A HREF="javascript:go(\''. urlencode($path) .'\');">'.$path.'</a></b><br>');
if ($action != "view")
{
echo ('<textarea cols=80 rows=25 name="area" wrap=off>'.htmlentities(join( '', file( $file))) .'</textarea>');
echo ('<input type="submit" value="save" name="action">');
}
else
{
echo ('<pre>');
$fcontents = file( $file );
while ( list( $line_num, $line ) = each( $fcontents ) ) {
echo "<b>$line_num</b>\t" . htmlspecialchars( $line );
}
echo ('</pre>');
echo ('<input type="submit" value="edit" name="action">');
}
}
if ($pw != "password")
{
echo ('<form action="'.getenv("SCRIPT_URL").'" method="POST">');
echo('Password:<INPUT type="password" name="pw">' );
echo('<INPUT type="submit">');
echo('</form></body></html>');
exit;
}
echo ('<script language="javascript">function go(p,f){ document.forms[0].path.value=p; document.forms[0].file.value=f;document.forms[0].submit();}</script>');
echo ('<form ENCTYPE="multipart/form-data" action="'.getenv("SCRIPT_URL").'" method="POST">');
echo ('<input type="hidden" name="file" value="'.$file.'">');
echo ('<input type="hidden" name="path" value="'.$path.'">');
echo ('<input type="hidden" name="pw" value="'.$pw.'">');
if ($action == "upload")
{
if ($userfile != null)
copy($userfile, $path.'/'.basename(str_replace('\\','/',stripslashes($userfile_name))));
}
else
if ($new == "New File")
{
if(!fopen($path . '/'. $text1, "w")) echo ("Couldn't create $text1<br>");
}
else if ($new == "New Dir")
{
if(!mkdir($path . '/' .$text1, 0777)) echo ("Couldn't create $text1<br>");
}
else
if ($action == "save")
{
$f = fopen($file, "w");
fputs($f, stripslashes($area));
fclose($f);
}
if ($action == "delete")
{
if ($file != null)
{
if(!unlink($file)) echo "Could not delete $file<br>";
}
else
{
if(!rmdir($path)) echo "Could not delete $path<br>";
$path = dirname($path);
}
}
else
if ($action == "copy")
{
if ($file != null)
{
if(!copy($file, $path .'/'.$text1)) echo("Could not copy $file to $text1<br>");
}
else
{
if (!copy($path, $path .'/'.$text1)) echo("Could not copy $path to $text1<br>");
}
}
else
if ($action == "rename")
{
if ($file != null)
{
if(!rename($file, $path .'/'.$text1)) echo("Could not rename $file to $text1<br>");
}
else
{
if(!rename($path, $path .'/'.$text1)) echo("Could not rename $path to $text1<br>");
}
}
if ($file != null && ($action =="view" || $action == "edit" || $action == "save"))
{
displayfile();
}
else
displaydir();
?>
</form>
</body>
</html>