loading and saving files?

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
 
thats ok but i need something that can change files without me having to set their permishions to writable. is there a php code i can add to change their permishions. i have one script which can create and edit files without me doing anything. i have included the code for you.

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


Thanks
 
The code that you posted doesn't do any special with permissions, as far as I can tell. In order for it to work, it must still have write permission for the file in question. This is an OS-level thing, not a PHP thing, and there's effectively no way around OS permissions. If your server process is running under a user account that has universal write access (e.g. Administrator), however, you won't need to change the permissions.

PHP does have a chmod() function which you may find useful, but of course, the PHP process must have chmod permission (which may or may not be the case).
 
all i did was give the file ftp.php file write permis and from that it has managed to create, delete, edit etc without me haveing to give the other files anything. i gave your script write permis but it does not work :(
 
As I see there's also a mkdir involved.
You may want to use the umask() function to assign directory permissions.
 
Back
Top