php学习--net类库函数

file_get_contents($url);//返回指定的web页面的所有内容
在php中,文件函数可以完成许多操作。对于某些http或https任务,可能需要curl库,
通过curl库可以登陆到一个web站点并且在一些页面中模拟用户的操作


使用网络查找函数
$url=parse_url("url"):该函数返回包含url不同部分的相关数组,该数组的可用信息部分分别是
“模式”,“用户”,“传递”,“主机”,“端口”,“路径”,“查询”和“代码段”(scheme,user,pass,host,port,path,query,fragment)
如:http://nobody:secret@example.com:80/script.php?variable=value#anchor,则数组中的值为
scheme:http
user:nobody
pass:secret
host:example.com
port:80
path:/script.php
query:variable=value
fragment:anchor


在完成url分析后,如果主机是在域名服务(DNS)中,可以获得此主机的viper地址,使用gethostbyname(),如果主机存在则返回ip地址,否则返回flase


分析email
$email=expload("@",$emailStr);
$ehost=$email[1];
使用dns_get_mx($ehost,$mxhostarr);检查是否有邮件可以到达的确切地方,windows不包含该函数,如果要检查email,需要安装perl,使用PEAR::Net_DNS包。


FTP
+展开
-HTML
<?php
$host="ftp.cs.rmit.edu.au";
$user="anonymous";
$password="me@example.com";
$remotefile='/pub/tsg/teraterm/ttssh14.zip';
$localfile='/tmp/writable/ttssh14.zip';
$cn=ftp_connect($host);
if($cn){
echo "Error:Count not connect to ftp server";
exit;
}

@$result=ftp_login($cn,$user,$password);
if(!$result){
echo "Error:Could not log on as $user";
ftp_quit($cn);
exit;
}
if(file_exists($localfile)){
$localtime=filemtiem($localfile);
echo 'local file last updated';
echo date('G:i j-M-Y',$localtime);
}
else $localtime=0;
$remotetime=ftp_mdtm($cn,$remotefile);
if(!($remotetime>=0)){
echo "can't access romote file time";
$remotetime=$localtime+1;
}
else{
echo 'Remote file last updated';
echo date('G:i j-M-Y',$remotetime);
}
if($locatime>=$remotetime){
echo "Local copy is up to date";
ftp_quit($cn);exit;
}
$fp=fopen($localfile,'w');
if(!ftp_fget($conn,$fp,$remotefile,FTP_BINARY){//FTP_BINARY/FTP_ASCII
echo "Error:Could not download file";
ftp_quit($conn);
fclose($fp);
exit;
}
echo "File downloaded successfully";
fclose($fp);
ftp_quit($cn);
?>


许多ftp支持匿名登陆(anonymous),不需要密码,但通常做法是把电子邮件作为密码,这样管理员知道用户来自什么地方。

下载
ftp_fget(int ftp_connection,int file_handler,string remote_filepath,int mode):需要打开文件
ftp_get(int ftp_connection,string local_filepath,string remote_filepath,int mode),不需要打开文件

mode:可以为FTP_BINARY或者FTP_ASCII,一般为FTP_BINARY

上传
int ftp_fput(int ftp_connection,string remote_filepath,int file_handler,int mode);
int ftp_put(int ftp_connection,string remote_filepath,string local_filepath,int mode);

设置超时
可以在php.ini中定义,默认为30s
使用set_time_limit()修改最大可执行时间(单位为秒)。



其他ftp函数
int ftp_size(int $ftp_cn,string remote_filepath):显示远程服务器上一个文件的大小,并非所有服务器都支持

array ftp_nlist($ftp_cn,string dir):显示特定目录中的文件名的列表,或组目录路径可以使用dirname($remote_filepath);

加支付宝好友偷能量挖...


原创文章,转载请注明出处:php学习--net类库函数

评论(0)Web开发网
阅读(118)喜欢(1)PHP/apache/Perl