Книжка "Профессиональное PHP программирование (2-е издание)" Глава 10. Кодирование клиентов FTP.
Делать было нечего, набрал пример, приведенный в книге. Опечатки, вроде все отловил, с принципом работы разобрался, перепроверил, не работает
Может кто-нить уже копался до меня в этом примере? В чем причина?
| Код | <? set_time_limit(0); if (getenv('SERVER_ADMIN')) { define('SERVER_ADMIN',getenv('SERVER_ADMIN')); } else { define('SERVER_ADMIN','root@'.getenv('SERVER_NAME')); }
class Ftp_Web_Client { var $conn, $cwd, $max_upload_size, $mode, $systype, $tmp_dir='/tmp'; function Ftp_Web_Client($host, $user='anonymous', $pass='$SERVER_ADMIN', $port=21) { $fh=ftp_connect($host, $port); if (!$fh) { user_error("Could not connect to host '$host:$port'"); return FALSE; } $logged_in=ftp_login($fh, $user, $pass); if (!$logged_in) { user_error("Could not authenticate to host '$host:$port'.\n"); return FALSE; } $this->conn=$fh; register_shutdown_function( create_function('', 'ftp_quit($fh);') ); $this->cwd=ftp_pwd($this->conn); $this->max_upload_size=1024*1024; $this->mode=FTP_BINARY; $this->systype=ftp_systype($this->conn); }
function cd($dir) { $return=ftp_chdir($this->conn, $dir); if ($return) { $this->cwd=ftp_pwd($this->conn); } else { user_error("Could not cd into directory '$dir'."); } return $return; }
function ls($directory='.') { $temp=ftp_rawlist($this->conn, $directory); if (FALSE===$temp) { user_error("This directory listings could not be retrieved."); return array(); } switch ($this->systype) { case 'Windows_NT': $re='/^'. '([0-9-]+\s+'. '\d\d:\d\d..)\s+'. '(\S+)\s+'. '(.+)$/'; break; case 'UNIX': default: $re='/'. '^(.)'. '(\S+)\s+'. '\S+\s+'. '(\S+)\s+'. '(\S+)\s+'. '(\S+)\s+'. '(\S+\s+\S+\s+\S+\s+)'. '(.+)$/'; break; } $type=array('-'=>'file', 'd'=>'directory'); $listings=array(); foreach ($temp as $entry) { $match=preg_match($re, $entry, $matches); if (!$match) { user_error("This directory listings could not be parsed."); return array(); } switch ($this->systype) { case 'Windows_NT': $listings[]=array('type'=>is_numeric($matches[2]) ? 'file' : 'directory', 'permissions'=>NULL, 'owner'=>NULL, 'group'=>NULL, 'size'=>(int)$matches[2], 'last mod'=>$matches[1], 'name'=>$matches[3] ); break;
case 'UNIX': default: $listings[]=array('type'=>$type[$matches[1]], 'permissions'=>$matches[2], 'owner'=>$matches[3], 'group'=>$matches[4], 'size'=>$matches[5], 'last mod'=>$matches[6], 'name'=>$matches[7] ); break; } } return $listings; }
function get($file) { $tmp_name=$this->tmp_dir.'/ftp_web_client_'.md5($file.microtime()); $result=ftp_get($this->conn, $tmp_name, $this->cwd."/$file",$this->mode);
if ($result) { header("Content-Type: application/octet-stream"); header("Content-Disposition: inline;'.'filename='.urlencode($file)); readfile($tmp_name); unlink($tmp_name); exit(); }
$clean=htmlentities($file); user_error("Could not download file '$clean'."); return FALSE; }
}
$ftp=new Ftp_Web_Client('ftp.wrox.com'); if ('Change to Selected Directory'==$_POST['action']) { $ftp->cd($_POST['dir']); } else if (isset($_POST['cwd'])) { $ftp->cd($_POST['cwd']); }
if ('Download Selected File'==$_POST['action']) { if (isset($_POST['selected_file'])) { $ftp->get($_POST['selected_file']); } else { user_error("Please select a file to download!"); } }
$directory=array(); $file=array(); foreach ($ftp->ls($ftp->cwd) as $entry) { switch ($entry['type']) { case 'directory': $directory[]=$entry['name'] ; break; default: $file[$entry['name']]=sprintf("%s (%0.2f kb)", $entry['name'], $entry['size']/1024); break;
} }
?>
<form action="<?echo (getenv('SCRIPT_NAME'));?>" enctype="multipart/form-data" method="POST"> <input type="hidden" name="cwd" value="<?echo (htmlentities(stripslashes ($ftp->cwd)));?>"/> <input type="hidden" name="max_file_size" value="<?echo ($ftp->max_file_size);?>"/> <p><b>Current Working Directory:</b><?echo ($ftp->cwd);?></p> <p> <select name="dir"> <? if ('/'==$ftp->cwd) { echo ("<option>/</option>"); } else {
echo ("<option value=\"{$ftp->cwd}\"> . ({$ftp->cwd})</option>\n", "<option value=\"{$ftp->cwd}/..\"> .. </option>\n");
}
foreach ($directory as $name => $entry) { printf( "<option value=\"%s\"">%s</option>'."\n", "{$ftp->cwd}/$name", $name ); } ?> </select> <input type="submit" name="action" value="Change to Selected Directory" /> </p> <p> <select name="selected_file" size="20"> <? foreach ($file as $name => $entry) { echo("<option value=\"$name\">$entry</option>\n"); } ?> </select><br /> <input type="submit" name="action" value="Download Selected File" /><be /> </form>
| |