Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

loading and saving files?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

loading and saving files?

Postby digitalbloke » Fri Jul 25, 2003 6:01 am

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
digitalbloke
New php-forum User
New php-forum User
 
Posts: 59
Joined: Tue Jul 01, 2003 7:02 am

Postby swirlee » Fri Jul 25, 2003 6:42 am

Here is a very succinct overview.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby digitalbloke » Fri Jul 25, 2003 8:27 am

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: Select all
<?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
digitalbloke
New php-forum User
New php-forum User
 
Posts: 59
Joined: Tue Jul 01, 2003 7:02 am

Postby swirlee » Fri Jul 25, 2003 8:46 am

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).
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby digitalbloke » Fri Jul 25, 2003 8:49 am

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 :(
digitalbloke
New php-forum User
New php-forum User
 
Posts: 59
Joined: Tue Jul 01, 2003 7:02 am

Postby Joan Garnet » Sat Jul 26, 2003 2:51 pm

As I see there's also a mkdir involved.
You may want to use the umask() function to assign directory permissions.
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.