php file_exists 函数与 file_exists语法
bool file_exists ( string filename )
如果由 filename 指定的文件或目录存在则返回 TRUE,否则返回 FALSE。
其实于php教程 file_exists 函数与 file_exists语法我们早就讲过了,下面我们来看看一下关于它的使用方法与实例吧
路径的文件或目录。
在Windows上,使用/ /计算机名/共享/文件名或 计算机名共享文件名,以检查网络共享文件。
这是一个很简单的实例一
<?php
$filename = '/www.111cn.net/aa/to/foo.txt';
if (file_exists($filename)) {
echo "文件$filename exists";
} else {
echo "文件$filename 不存在";
}
?>
输出结果为:
文件/www.111cn.net/aa/to/foo.txt己存在
再来看看实例二
<?php
echo file_exists("www.111cn.net.txt");
?>
这个我们就直接用file_exists来返回ture or false
try{
$tor = new TorHttp('127.0.0.1', 9050);
$data = array('username' => 'liujun');
$headers = array('Cookie' => 'php教程id=123456; xx=v');
echo $tor->get('http://host.com/testsocks.php', $headers);
$tor->newId('123456');
}catch(Exception $e){
if($e->getCode() == 9999){
$tor->newId('123456');
}
echo $e->getMessage() . "n";
}
*/
class Tool_TorHttp
{
/**
* Tor提供的socks服务器
*
* @var <string
*/
private $_host;
/**
* socks服务的连接
*
* @var <stream>
*/
private $_sock;
/**
* 构造函数
*
* @param <string> $host socks服务器地址
* @param <int> $port
*/
public function __construct($host = '127.0.0.1', $port = 9050)
{
$this->_host = $host;
@ $this->_sock = fsockopen($host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器');
}
//建立应用层的连接
fwrite($this->_sock, pack('C3', 5, 1, 0));
$resp = fread($this->_sock, 1024);
$resp = unpack('Cversion/Cmethod', $resp);
if($resp['version'] != 5 || $resp['method'] != 0){
$this->_sock = null;
throw new Exception('代理服务器不可用或者需要连接密码');
}
}
/**
* 连接目标主机
*
* @param <type> $host
* @param <type> $port
*/
private function _connect($host, $port)
{
//ip和域名描述的服务器用不同的报文格式
$lip = ip2long($host);
if(empty($lip)){
$pack = pack('C5', 5, 1, 0, 3, strlen($host)) . $host . pack('n', intval($port));
}else{
$pack = pack('C4Nn', 5, 1, 0, 1, $lip, $port);
}
fwrite($this->_sock, $pack);
$resp = '';
$counter = 0;
while(true){
if(feof($this->_sock)){
break;
}
$resp .= fread($this->_sock, 1024);
if(!empty($resp) || $counter == 50){
break;
}
$counter += 1;
}
$resp = unpack('Cversion/Crep', $resp);
if(($resp['version'] != 5) || ($resp['rep'] != 0)){
throw new Exception("请求的服务[$host:$port]暂时不可用,或者Tor不能到达,如果反复发生,请尝试重启Tor", 9999);
}
}
/**
* 发起一次http的get请求
*
* @param <type> $url
* @return <string>
*/
public function get($url, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
$requestUri = $ua['path'];
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
$headers['Host'] = $ua['host'];
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
fwrite($this->_sock, "GET {$requestUri} HTTP/1.1n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 发起一次http的post请求
*
* @param <type> $url
* @param <type> $data
* @param <type> $headers
* @return <type>
*/
public function post($url, $data, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
if(isset($ua['path']) && !empty($ua['path'])){
$requestUri = $ua['path'];
}else{
$requestUri = '/';
}
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
$data = $this->_parseData($data);
fwrite($this->_sock, "POST {$requestUri} HTTP/1.1n");
fwrite($this->_sock, "Host: {$ua['host']}n");
fwrite($this->_sock, "Content-Type: application/x-www-form-urlencodedn");
fwrite($this->_sock, "Content-Length: " . strlen($data) . "n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
fwrite($this->_sock, $data . "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 更新Tor的身份
*
* @param <type> $password
* @param <type> $port
* @return <type>
*/
public function newId($password = '', $port = 9051)
{
if(!empty($password) && $password[0] != '"'){
$password = '"' . $password . '"';
}
//创建到tor控终端的连接
@ $sock = fsockopen($this->_host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器控制端,请检查端口号');
}
fwrite($sock, "AUTHENTICATE {$password}n");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('Tor控制认证失败,请确认密码正确');
}
fwrite($sock, "SIGNAL NEWNYMn");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('更新身份失败,请重试');
}
return true;
}
private function _parseData($data)
{
if(empty($data) || !is_array($data)){
return $data;
}
$encoded = '';
while (list($k, $v) = each($data)) {
$encoded .= $k . "=" . $v . '&';
}
return substr($encoded, 0, -1);
}
public function __destruct()
{
fclose($this->_sock);
$this->_sock = null;
}
}
PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等)。该函数将当前为止程序的所有输出发送到用户的浏览器。
flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响。因此,必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
个别web服务器程序,特别是Win32下的web服务器程序,在发送结果到浏览器之前,仍然会缓存脚本的输出,直到程序结束为止
*/
for ($i=10; $i>0; $i--)
{
echo $i;
ob_flush();
flush();
sleep(1);
}
// Microsoft Internet Explorer 只有当接受到的256个字节以后才开始显示该页面,所以必须发送一些额外的空格来让这些浏览器显示页面内容
//正常用法
ob_start();
ob_end_flush();
for($i = 1; $i <= 300; $i++ )echo ' www.111cn.net';
$i = 0;
while(1) {
echo $i;
sleep(1);
$i++;
}
for($i=0;$i<10;$i++) {
echo $i;
flush();
sleep(1);
}
//这段代码,应该隔一秒钟输出一次$i,但是实际中却不一定是这样,IE浏览器下有可能是等了10秒钟后,所有的输出同时呈现出来
//ob_end_flush();//IE8下没起作用
echo str_pad(" ", 256);//IE需要接受到256个字节之后才开始显示
for($i=0;$i<18;$i++) {
echo $i;
flush();
sleep(1);
}
<?xml version="1.0" encoding="gb2312"?>
<rss version="2.0">
<channel>
<title>javascript教程</title>
<link>http://blog.111cn.net/zhongmao/category/29515.asp教程x</link>
<description>javascript</description>
<language>zh-chs</language>
<generator>.text version 0.958.2004.2001</generator>
<item>
<creator>zhongmao</creator>
<title orderby="1">out put excel used javascript</title>
<link>http://blog.111cn.net/zhongmao/archive/2004/09/15/105385.aspx</link>
<pubdate>wed, 15 sep 2004 13:32:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/09/15/105385.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/105385.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/09/15/105385.aspx#feedback</comments>
<comments>2</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/105385.aspx</commentrss>
<ping>http://blog.111cn.net/zhongmao/services/trackbacks/105385.aspx</ping>
<description>test description</description>
</item>
<item>
<creator>zhongmao</creator>
<title orderby="2">out put word used javascript</title>
<link>http://blog.111cn.net/zhongmao/archive/2004/08/06/67161.aspx</link>
<pubdate>fri, 06 aug 2004 16:33:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/08/06/67161.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/67161.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/08/06/67161.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/67161.aspx</commentrss>
<ping>http://blog.111cn.net/zhongmao/services/trackbacks/67161.aspx</ping>
<description>test word description</description>
</item>
<item>
<creator>zhongmao</creator>
<title orderby="3">xmlhttp</title>
<link>http://blog.111cn.net/zhongmao/archive/2004/08/02/58417.aspx</link>
<pubdate>mon, 02 aug 2004 10:11:00 gmt</pubdate>
<guid>http://blog.111cn.net/zhongmao/archive/2004/08/02/58417.aspx</guid>
<comment>http://blog.111cn.net/zhongmao/comments/58417.aspx</comment>
<comments>http://blog.111cn.net/zhongmao/archive/2004/08/02/58417.aspx#feedback</comments>
<comments>0</comments>
<commentrss>http://blog.111cn.net/zhongmao/comments/commentrss/58417.aspx</commentrss>
<ping>http://blog.111cn.net/zhongmao/services/trackbacks/58417.aspx</ping>
<description>xmlhttpaaa asd bb cc dd</description>
</item>
</channel>
</rss>
<?
//首先要创建一个DOMDocument对象
$dom = new DomDocument();
//然后载入XML文件
$dom -> load("test.xml");
//输出XML文件
//header("Content-type: text/xml;charset=gb2312");
//echo $dom -> saveXML();
//保存XML文件,返回值为int(文件大小,以字节为单位)
//$dom -> save("newfile.xml");
echo "<hr/>取得所有的title元素:<hr/>";
$titles = $dom -> getElementsByTagName("title");
foreach ($titles as $node){
echo $node -> textContent . "<br/>";
//这样也可以
//echo $node->firstChild->data . "<br/>";
}
/*
echo "<hr/>从根结点遍历所有结点:<br/>";
foreach ($dom->documentElement->childNodes as $items) {
//如果节点是一个元素(nodeType == 1)并且名字是item就继续循环
if ($items->nodeType == 1 && $items->nodeName == "item") {
foreach ($items->childNodes as $titles) {
//如果节点是一个元素,并且名字是title就打印它.
if ($titles->nodeType == 1 && $titles->nodeName == "title") {
print $titles->textContent . "n";
}
}
}
}
*/
//使用XPath查询数据
echo "<hr/>使用XPath查询的title节点结果:<hr/>";
$xpath = new domxpath($dom);
$titles = $xpath->query("/rss/channel/item/title");
foreach ($titles as $node){
echo $node->textContent."<br/>";
}
/*
这样和使用getElementsByTagName()方法差不多,但是Xpath要强大的多
深入一点可能是这样:
/rss/channel/item[position() = 1]/title 返回第一个item元素的所有
/rss/channel/item/title[@id = '23'] 返回所有含有id属性并且值为23的title
/rss/channel/&folder&/title 返回所有articles元素下面的title(译者注:&folder&代表目录深度)
*/
//向DOM中写入新数据
$item = $dom->createElement("item");
$title = $dom->createElement("title");
$titleText = $dom->createTextNode("title text");
$title->appendChild($titleText);
$item->appendChild($title);
$dom->documentElement->getElementsByTagName('channel')->item(0)->appendChild($item);
//从DOM中删除节点
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName("channel")->item(0));
//或者使用xpath查询出节点再删除
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel")->item(0));
//$dom->save("newfile.xml");
//从DOM中修改节点数据
//修改第一个title的文件
//这个地方比较笨,新创建一个节点,然后替换旧的节点。如果哪位朋友有其他好的方法请一定要告诉我
$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
$newTitle = $dom->createElement("title");
$newTitle->appendChild(new DOMText("This's the new title text!!!"));
$firstTitle->parentNode->replaceChild($newTitle, $firstTitle);
//修改属性
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
//$firstTitle->setAttribute("orderby", "4");
$dom->save("newfile.xml");
echo "<hr/><a href="newfile.xml">查看newfile.xml</a>";
//下面的代码获得并解析111cn.net的首页,将返第一个title元素的内容。
/*
$dom->loadHTMLFile("http://www.111cn.net/");
$title = $dom->getElementsByTagName("title");
print $title->item(0)->textContent;
*/
?>
在PHP中使用的是执行相同的代码集的次数。作者的基本语法如下:
for循环只是一个多一点的代码,同时循环添加到它。而所涉及的一个循环的共同任务是:
设置一些计数器变量的初始值。
请检查条件语句是正确的。
执行中的代码循环。
增量在每个迭代结束通过循环计数器。
for循环允许你定义一个简单的代码行这些步骤。它似乎有一种奇怪的形式,所以一定要密切注意语法用!
FOR (expression 1, expression 2, expression 3)
{
[code to execute]
}
for ( initialize a counter; conditional statement; increment a counter){
do this code;
}
看简单实例一
FOR ($i = 0; $i <= 2; $i++)
{
print "value is now " . $i . "<br>";
}
输出值
value is now 0
value is now 1
value is now 2
在第一循环,$i=0,这意味着表达,($i<= 2),为ture。因此,打印语句执行时,$i得到加1,变成1。
在第二循环,$ = 1,这意味着表达,($i<= 2),为ture。因此,打印语句执行时,$i得到加1,变成2。
在第三迭代,$i= 2,这意味着表达,($i<= 2),为ture。因此,打印语句执行时,$i递增,成为1 3。
在第四迭代,$i= 3,这意味着表达,($i<= 2),是假的。因此,PHP不去执行循环,不执行打印语句。
实例二
$brush_price = 5;
echo "<table border="1" align="center">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
for ( $counter = 10; $counter <= 100; $counter += 10) {
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $brush_price * $counter;
echo "</td></tr>";
}
echo "</table>";
输出值
Quantity Price
10 50
20 100
30 150
40 200
50 250
60 300
70 350
80 400
90 450
100 500
文章原创于www.111cn.net
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了Vue 3.0 中 jsx 语法使用,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下...2020-11-13
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
SQL Server中row_number函数的常见用法示例详解
这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21- 下面小编就为大家带来一篇C#中加载dll并调用其函数的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#虚函数用法,实例分析了C#中虚函数的功能与基本使用技巧,需要的朋友可以参考下...2020-06-25