Page 1 of 1

PHP File Manager

Posted: Fri Jul 15, 2005 2:51 pm
by Leys
Basically, there are a few things that don't work. Once you travel beyond the main directory, everything is recognized as just a file even if its a dir. This code is extremely slow when trying to move into somewhat populated directory's and im not sure how to make it at least fast enough to actually work. Finally, it fails to get the contents of a file to the textarea in edit mode.

Code:

Code: Select all

<?
function ftp_is_dir($conn, $dir)
{
 if(@ftp_chdir($conn, $dir))
 {
  ftp_chdir($conn, &quote;..&quote;);
  return true;
 } else {
  return false;
 }
}
?>
<html>
<head>
<title>ProjectLance.net - File Manager</title>
<link rel=&quote;stylesheet&quote; type=&quote;text/css&quote; href=&quote;style.css&quote;/>
</head>

<body>
<?
//LOGIN INFO
 $dom=&quote;davidzworld.programmer-scripts.com&quote;;
 $domain=&quote;ftp.davidzworld.programmer-scripts.com&quote;;
 $user=&quote;*****&quote;;
 $pass=&quote;*****&quote;;

//SET CONNECTIONS
 $conn_id=ftp_connect($domain);
 $login = ftp_login($conn_id, $user, $pass); 
 ftp_pasv ( $conn_id, true );

//DIRECTORY CONTENTS
if($_GET&#1111;&quote;dir&quote;]!=&quote;&quote;)
{
 $mydir=$_GET&#1111;&quote;dir&quote;];
} else {
 $mydir=&quote;.&quote;;
}
 ftp_chdir($conn_id, $mydir);
 $contents = ftp_nlist($conn_id, &quote;.&quote;);
?>
<? if($_GET&#1111;&quote;mode&quote;]==&quote;view&quote; || $_GET&#1111;&quote;mode&quote;]==&quote;&quote;) { ?>
<center>
<table width=&quote;800&quote; cellpadding=&quote;2&quote; cellspacing=&quote;0&quote; style=&quote;border: 1px solid #000;&quote;>
<tr>
<td class=&quote;tblHead&quote;>File Manager: <i><?= $dom; ?></i></td>
</tr>
<?
 $sColor=&quote;#6666AA&quote;;
 foreach($contents as $dir)
 {
  if(ftp_is_dir($conn_id, $dir) && $dir!=&quote;.&quote; && $dir!=&quote;..&quote;)
  {
   if($sColor==&quote;#6666AA&quote;) { $sColor=&quote;#666699&quote;; } else { $sColor=&quote;#6666AA&quote;; }
   echo &quote;<tr>\n&quote;;
   echo &quote;<td bgcolor='$sColor' style='border-top: 1px solid #000;'><a href='main.php?mode=view&dir=$dir'><img src='images/folder.gif' border='0'/></a> $dir</td>\n&quote;;
   echo &quote;</tr>\n&quote;;
  }
 }
 foreach($contents as $file)
 {
  if(!ftp_is_dir($conn_id, $file))
  {
   if($sColor==&quote;#6666AA&quote;) { $sColor=&quote;#666699&quote;; } else { $sColor=&quote;#6666AA&quote;; }
   echo &quote;<tr>\n&quote;;
   echo &quote;<td bgcolor='$sColor' style='border-top: 1px solid #000;'><a href='main.php?mode=edit&file=$file'><img src='images/file.gif' border='0'/></a> $file</td>\n&quote;;
   echo &quote;</tr>\n&quote;;
  }
 }
?>
</table>
</center>
<? } else if($_GET&#1111;&quote;mode&quote;]==&quote;edit&quote;) { ?>
<center>
<table width=&quote;800&quote; cellpadding=&quote;2&quote; cellspacing=&quote;0&quote; style=&quote;border: 1px solid #000;&quote;>
<tr>
<td class=&quote;tblHead&quote; align=&quote;center&quote;>Edit File: <?= $_GET&#1111;&quote;file&quote;]; ?></td>
</tr>
<tr>
<td bgcolor=&quote;#66666AA&quote; align=&quote;center&quote;>
<form action=&quote;main.php&quote; method=&quote;post&quote;>
<textarea name=&quote;FContents&quote; rows=&quote;30&quote; cols=&quote;70&quote;>
<?
if (ftp_get($conn_id, getcwd().&quote;/&quote;.$file, $file, FTP_BINARY)) {
 $INSIDE=file_get_contents($file);
 echo nl2br($INSIDE);
} else {
 echo &quote;Could Not Get Contents...&quote;;
}
?>
</textarea>
</form>
</td>
</tr>
<? } ?>
<?
//CLOSE CONNECTION
 ftp_close($conn_id);
?>
</body>

</html>
Live Example:
http://www.davidzworld.programmer-scrip ... r/main.php

Any help would be greatly appreciated.

Posted: Fri Jul 15, 2005 3:34 pm
by x4t
Hi,

I think the FTP functions of PHP are rather slow, but I don't know for sure. Maybe you can do with a 'normal' file browser?

x4t

Posted: Fri Jul 15, 2005 3:43 pm
by Leys
well, thats the only way i know how to do it. The file manager is located off site from the actual files, so im not sure how else i can do it...

Posted: Fri Jul 15, 2005 3:44 pm
by pickle
One thing I might suggest is not changing your directory. You can just as easily update the path of the directory your trying to open, and it has less overhead.

Is the purpose of this script to be applicable to an arbitrary server or is it solely to access files on the server on which the script is running? If the situation is the latter, why not use file system calls?

Edit: never mind about the last paragraph.

Posted: Fri Jul 15, 2005 3:47 pm
by Leys
Well, it is being accessed from a remote server, so i think it has to go through ftp. What do you mean by just updating the path to open? like instead of opening "." , open the full path? I have tried that and it does not work and better than the current. I just need to figure out how to get this all working properly.

Posted: Fri Jul 15, 2005 3:47 pm
by x4t
pickle wrote:One thing I might suggest is not changing your directory. You can just as easily update the path of the directory your trying to open, and it has less overhead.

Is the purpose of this script to be applicable to an arbitrary server or is it solely to access files on the server on which the script is running? If the situation is the latter, why not use file system calls?
*Agrees ^^ I think you can better use a FTP script if you want to access remote scripts. For scripts on your own server, I'd use PHPs dir class or PHPs dir functions.

Posted: Fri Jul 15, 2005 3:56 pm
by pickle
Say you login and your current directory is /home/me/. You then want to access /home/me/like/cookies.yum. Rather than calling ftp_chdir() and moving to /home/me/like/ and calling ftp_open('cookies.yum'), just update the string in your script to reflect the new address. So, just tack '/home/me/like/' in front of the filename 'cookies.yum'. That will save you one ftp transaction.

I'm sure this is meaningless to you, but congratulations on being my 1000th post

Posted: Fri Jul 15, 2005 4:07 pm
by Leys
ok, I have done what you said. But there is no recognizable change. It still won't even open some directories at all.

Posted: Fri Jul 15, 2005 4:30 pm
by pickle
Get your is_dir function to output the entire string of the directory it's trying to check. My guess is that it's not exactly what you think it is.

Posted: Fri Jul 15, 2005 9:22 pm
by Leys
Ok, I have it mostly figured out now. But a few new things...

How do I upload a file to the ftp server? I can't seem to get it working...with ftp_put. Could someone please give me an example(with actual values)?

Also, how can i retrieve all the contents of a page so it can be edited through the file manager?

Thanks for the help :D

Posted: Mon Jul 18, 2005 9:49 am
by pickle
Here's something I've got using FTP (obviously with some values changed):

Code: Select all

$conn = ftp_connect($server_ip);
$error = (!@ftp_login($conn,REMOTE_ACCOUNT_NAME,REMOTE_ACCOUNT_PASSWORD)) 
                   ? "Could not connect to authentication server.  Please contact ABC at x123" 
                   : false;

$error = (!ftp_put($conn,'/etc/dir/users/'.$username.'/file.rules','/tmp/'.$username .'.tmp',FTP_ASCII))
                   ? "Could not upload authentication rules.  Please contact ABC at x123" 
                   : false;

ftp_close($conn);
ftp_get() should be able to allow you to download the entire contents of a file into a local file. ftp_fget() can save the contents of an ftp'd file into an open file handler - could save you some time.