php常见简单功能及函数

获取远程文件(图片等)

有三种方式

1.file_get_contents($url);使用file_get_contents()函数获取文件,在用file_put_contents()函数把文件写到本地。

2.使用curl

//获取到文件

$ch=curl_init();

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);

$file=curl_exec($ch);curl_close($ch);//写入本地$fp=fopen($save_dir.$filename,'a');

fwrite($fp,$file);

fclose($fp);

3.使用ob_start()

//获取文件ob_start();//打开缓冲区

readfile($url);

$file=ob_get_contents();

ob_end_clean();//写入本地

$fp=fopen($save_dir.$filename,'a');

fwrite($fp,$file);

fclose($fp);

多个进程写入同一个文件(加锁)

$fp =fopen("lock.txt","w+");

if(flock($fp,LOCK_EX)){//获得写锁,写数据

fwrite($fp,"write something");//解除锁定

flock($fp,LOCK_UN);

}else{   

 echo"file is locking";

}

fclose($fp);

跳转的方法

header('Location:http://www.baidu.com');//立刻跳转header('refresh:3;url=http://www.baidu.com');//三秒后跳转//php函数跳转缺点:执行前不能有输出

你可能感兴趣的:(php常见简单功能及函数)