当前位置:  编程技术>php
本页文章导读:
    ▪php中ftp_put无法上传文件的解决方法(图文)      在php中使用ftp函数进行文件上传功能的开发,可以正常连接到ftp服务器端,但就是无法上传文件。 程序代码如下: <?php //测试ftp上传文件 //by www. if ($_POST['action']=='上传') { $tmpname = $_FIL.........
    ▪一个用mysql内存表来代替php session的类      代码如下:   代码示例: <?php  /** * session mysql内存表 @Usage: use some other storage method(mysql or memcache) instead of php sessoin @author:lein @Version:1.2 */  session_start();  if(!isset($_SESSION['test'])){  .........
    ▪php在某年月日之后过期(不受服务器本身时间设置影响)的代码分享      代码如下: <?php /** * php在指定时间内过期 * edit by www. */ while(! ($dataFile = @fopen('http://www./6892.html', "r" )) ){ sleep(2); } //$dataFile = fopen('data.txt', "r" ) ; $buffer = ''; if ( $dataFile ) { $buffer = ".........

[1]php中ftp_put无法上传文件的解决方法(图文)
    来源: 互联网  发布时间: 2013-12-24

在php中使用ftp函数进行文件上传功能的开发,可以正常连接到ftp服务器端,但就是无法上传文件。

程序代码如下:

<?php
//测试ftp上传文件
//by www.
if ($_POST['action']=='上传')
{
$tmpname = $_FILES['file']['tmp_name'];
$filename = $_FILES["file"]["name"];

$conn_id = ftp_connect('192.168.11.186') or die('连接失败');
$ftp_user = 'user1';
$ftp_pass = 'xxxxxx';
if(! @ftp_login($conn_id, $ftp_user, $ftp_pass)){
    echo '登录失败<br/>';  
} else {
    echo "连接成功<br/>";
}

echo ftp_pwd();

//ftp_pasv($conn_id, true);
$remote = '/d/file/' . $filename;
$local = $tmpname;

if(ftp_put($conn_id, $remote,$local, FTP_BINARY)){
    echo '图片ftp上传成功 '.$local . "<br/>";;
}  else {
   echo '图片 Ftp 上传失败' . $local . " - " . $remote . "<br/>";
}
    
ftp_close($conn_id);
}
?>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" >
        <form action="/blog_article/do.html" method="post" enctype="multipart/form-data" name="etform">         
          <tr bgcolor="#FFFFFF"> 
            <td>本地上传</td>
            <td><input name="file" type="file" size="32"> </td>
          </tr>          
          <tr bgcolor="#FFFFFF"> 
            <td>&nbsp;</td>
            <td><input type="submit" name="action" value="上传"></td>
          </tr>
        </form>
      </table>

运行结果:

使用ftp命令测试到ftp服务端的连接如下:


由上图可知,连接是正常的。

可是为什么不能正常上传文件呢?

首先,尝试了如下的方法。

1,开启apache中针对当前站点的错误日志记录功能。


2,运行php程序,继续测试ftp上传功能。

3,查看错误日志,发现如下错误信息:
 

原来,在linux系统中,php配置文件中的上传文件的临时文件目录,如果没有配置的话,默认为/tmp/。
默认情况下,该目录没有网站的可写入权限,因此不能存储临时文件$_FILES[file]['tmp_name'],所以上传失败。

找到原因了,解决办法就简单了,用vim打开/etc/php.ini中,作如下修改:

去掉upload_tmp_dir前的注释分号,然后定上临时文件的目录即可。

最后记得加上apache站点用户的可写权限:
 

代码示例:
#chown -R www-data.www-data /var/www/tmp/
#chmod -R 777 /var/www/tmp/

再次上传,成功。

至此,在php中使用ftp_put不能上传文件的问题得以解决。

此问题的关键点:临时上传目录要给予权限。


    
[2]一个用mysql内存表来代替php session的类
    来源: 互联网  发布时间: 2013-12-24

代码如下:
 

代码示例:
<?php 
/**
* session mysql内存表
@Usage: use some other storage method(mysql or memcache) instead of php sessoin
@author:lein
@Version:1.2
*/ 
session_start(); 
if(!isset($_SESSION['test'])){ 
 $_SESSION['test']="123_lein_".date("Y-m-d H:i:s"); 

 
class session{     
    //session data 
    private $data; 
    //engine,mysql or memcache 
    private $engine; 
    //php session expire time 
    private $sessionexpiredTime; 
    //current user's session cookie value 
    private $sessionID; 
    //session coolie name 
    private $sessionCookieName; 
    public function session($engineBase=NULL,$engineName='mysql',$storage_name='php_session'){ 
        try{ 
            $this->sessionexpiredTime = intval(ini_get("session.cache_expire"))*10;//默认是180分钟,太长了,改为了30分钟 
        }catch(Exception $Exception){ 
            $this->sessionexpiredTime = 1200; 
        } 
        try{ 
            $this->sessionCookieName = ini_get("session.name"); 
        }catch(Exception $Exception){ 
            $this->sessionCookieName = 'PHPSESSID'; 
        } 
         
        if(!isset($_COOKIE[$this->sessionCookieName])){ 
            @session_start(); 
            $this->sessionID=session_id(); 
        }else{ 
            $this->sessionID=$_COOKIE[$this->sessionCookieName]; 
        } 
        $className = $engineName."SessionEngine"; 
        $this->engine = new $className( 
                                    array( 
                                          'storage_name'=>$storage_name,//mysql table name or memcahce key which stores data; 
                                          'expire_time'=>$this->sessionexpiredTime, 
                                          'data_too_long_instead_value' => '{__DATA IS *$* TO LONG__}' 
                                          ), 
                                    $this->sessionID, 
                                    &$engineBase 
                                    ); 
        $this->init(); 
        $this->loadFromSession(); 
        $this->engine->refresh(); 
        $this->engine->cleanup();      
    } 
    private function init() 
    { 
        $this->data = $this->engine->get(); 
        if(empty($this->data)){ 
            @session_start(); 
            if(!empty($_SESSION)){ 
                $this->data = $_SESSION; 
                $this->engine->create(false, $this->data); 
            } 
            else 
            { 
                $this->engine->create(false, ""); 
            } 
        } 
    } 
    public function loadFromSession($flagStartSession = false){ 
        $flag=false; 
        if($flagStartSession){ 
            @session_start(); 
        } 
        if($_SESSION&&is_array($_SESSION)){ 
            foreach($_SESSION as $k=>$v){ 
                if(!isset($this->data[$k])){ 
                    $this->data[$k] = $v; 
                    $flag=true; 
                } 
            } 
        } 
        if($flag){ 
            $this->engine->set(false, $this->data); 
        } 
    } 
    private function __get($nm) 
    { 
        if (isset($this->data[$nm])) { 
            $r = $this->data[$nm];            
            return $r; 
        }  
        else  
        { 
            return NULL; 
        } 
    } 
    private function __set($nm, $val) 
    { 
       $this->data[$nm] =  $val; 
       $this->engine->set(false, $this->data); 
    } 
     
    private function __isset($nm) 
    { 
        return isset($this->data[$nm]); 
    } 
 
    private function __unset($nm) 
    { 
        unset($this->data[$nm]); 
        $this->engine->set(false, $this->data); 
    } 
     
    function __destruct(){ 
        $this->data = NULL; 
        $this->engine->close(); 
        $this->engine = NULL; 
    } 

 
interface SessionEngine 

    /*
     * set varibles
     * @param $arr array,array(varible name=>varible value,...)
     */ 
    public function setVariable($arr); 
    /*
     * get session value
     * @param $key string
     */ 
    public function get($key=""); 
    /*
     * set session value
     * @param $key string
     * @param $value string
     */ 
    public function set($key="",$value=""); 
    /*
     * set session value
     * @param $key string
     * @param $value string
     */ 
    public function create($key="",$value=""); 
    /*
     * update the session's invalid time
     * @param $key string
     */ 
    public function refresh($key=""); 
    /*
     * close mysql or memcache connection
     */ 
    public function close(); 
    /*
     * delete expired sessions
     */ 
    public function cleanup(); 

 
final class mysqlSessionEngine implements SessionEngine{ 
    private $id=""; 
    private $storage_name='php_session'; 
    private $storage_name_slow='php_session_slow'; 
    private $data_too_long_instead_value = '{__DATA IS ~ TO LONG__}';//if data is longer than $max_session_data_length and you are using mysql 4 or below,insert this value into memery table instead. 
    private $expire_time=1200; 
    private $max_session_data_length = 2048; 
    private $conn; 
    private $mysql_version; 
    public function mysqlSessionEngine($arr=array(),$key="",&$_conn){ 
        $this->setVariable($arr); 
        $this->id = $key; 
        if(empty($this->id)||strlen($this->id)!=32){ 
            throw new Exception(__FILE__."->".__LINE__.": Session's cookie name can't be empty and it must have just 32 charactors!"); 
        } 
        $this->conn = $_conn; 
        if(!$this->conn||!is_resource($this->conn)){ 
            throw new Exception(__FILE__."->".__LINE__.": Need a mysql connection!"); 
        } 
        $this->mysql_version = $this->getOne("select floor(version())"); 
        if($this->mysql_version<5){ 
            $this->max_session_data_length = 255; 
        } 
    }    
    public function setVariable($arr){ 
        if(!empty($arr)&&is_array($arr)){ 
            foreach($arr as $k=>$v){ 
                $this->$k = $v;               
                if($k=='storage_name'){ 
                    $this->storage_name_slow = $v.'_slow'; 
                } 
            } 
        } 
    } 
    public function get($key=""){ 
        if($key=="") $key = $this->id; 
        $return = $this->getOne('select value from '.$this->storage_name.' where id="'.$key.'"'); 
        if($return==$this->data_too_long_instead_value) 
        { 
            $return = $this->getOne('select value from '.$this->storage_name_slow.' where id="'.$key.'"'); 
        } 
        if(!$return) 
        { 
            $mysqlError = mysql_error($this->conn); 
            if(strpos($mysqlError,"doesn't exist")!==false) 
            { 
                $this->initTable(); 
            } 
            $return = array(); 
        } 
        else 
        { 
            $return = unserialize($return); 
        } 
        return $return; 
    } 
    public function close(){ 
        @mysql_close($this->conn); 
    } 
    public function cleanup(){ 
        if($this->mysql_version>4){ 
            $sql = 'delete from '.$this->storage_name.' where date_add(`time`,INTERVAL '.$this->expire_time.' SECOND)<CURRENT_TIMESTAMP()'; 
        }else{ 
            $sql = 'delete from '.$this->storage_name_slow.' where `time`+'.$this->expire_time.'<unix_timestamp()'; 
            if($_SESSION['username']=="leinchu"){ 
                echo $sql; 
            } 
            $this->execute($sql); 
            $sql = 'delete from '.$this->storage_name.' where `time`+'.$this->expire_time.'<unix_timestamp()'; 
            if($_SESSION['username']=="leinchu"){ 
                echo $sql; 
            } 
        } 
        $this->execute($sql); 
    } 
    public function refresh($key=""){ 
        if($this->mysql_version>4){ 
            $sql = 'update '.$this->storage_name.' set `time`=CURRENT_TIMESTAMP() where id="'.$key.'"'; 
        }else{ 
            $sql = 'update '.$this->storage_name.' set `time`=unix_timestamp() where id="'.$key.'"'; 
        } 
        $return = $this->execute($sql); 
        if(!$return){ 
            $this->initTable(); 
            $return = $this->execute($sql,true); 
        } 
        return $return; 
    } 
    public function create($key="",$value=""){ 
        if($key=="") $key = $this->id; 
        if($value != "") $value = mysql_real_escape_string(serialize($value),$this->conn); 
        if(strlen($value)>$this->max_session_data_length) 
        { 
            if($this->mysql_version>4){ 
                throw new Exception(__FILE__."->".__LINE__.": Session data is long than max allow length(".$this->max_session_data_length.")!"); 
            } 
        } 
        if($this->mysql_version>4){ 
            $sql = 'replace into '.$this->storage_name.' set value=/''.$value.'/',id="'.$key.'",`time`=CURRENT_TIMESTAMP()'; 
        }else{ 
            $sql = 'replace into '.$this->storage_name.' set value=/''.$value.'/',id="'.$key.'",`time`=unix_timestamp()'; 
        } 
        $return = $this->execute($sql); 
        if(!$return){ 
            $this->initTable(); 
            $return = $this->execute($sql,true); 
        } 
        return $return; 
    } 
    public function set($key="",$value=""){      
        if($key=="") $key = $this->id; 
        if($value != "") $value = mysql_real_escape_string(serialize($value),$this->conn); 
        $sql = 'update '.$this->storage_name.' set value=/''.$value.'/' where id="'.$key.'"'; 
        if(strlen($value)>$this->max_session_data_length) 
        { 
            if($this->mysql_version>4){ 
                throw new Exception(__FILE__."->".__LINE__.": Session data is long than max allow length(".$this->max_session_data_length.")!"); 
            } 
            $sql = 'replace into '.$this->storage_name_slow.' set value=/''.$value.'/',id="'.$key.'",`time`=unix_timestamp()'; 
            $this->execute($sql,true); 
            $sql = 'update '.$this->storage_name.' set value=/''.$this->data_too_long_instead_value.'/' where id="'.$key.'"';  
        } 
        $return = $this->execute($sql); 
        if(!$return){ 
            $this->initTable(); 
            $return = $this->execute($sql,true); 
        } 
        return $return; 
    } 
    private function initTable(){        
        if($this->mysql_version>4){ 
            $sql = " 
                CREATE TABLE if not exists `".$this->storage_name."` ( 
                  `id` char(32) NOT NULL default 'ERR', 
                  `value` VARBINARY(".$this->max_session_data_length.") NULL, 
                  `time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
                  PRIMARY KEY  (`id`), 
                  KEY `time` (`time`) 
                ) ENGINE=MEMORY; 
                "; 
        }else{           
            $sqlSlow = " 
                CREATE TABLE if not exists `".$this->storage_name."_slow` ( 
                  `id` char(32) NOT NULL default 'ERR', 
                  `value` text NULL, 
                  `time` int(10) not null default '0', 
                  PRIMARY KEY  (`id`), 
                  KEY `time` (`time`) 
                ) ENGINE=MyISAM; 
                "; 
            $this->execute($sqlSlow,true); 
             
            $sql = " 
                CREATE TABLE if not exists `".$this->storage_name."` ( 
                  `id` char(32) NOT NULL default 'ERR', 
                  `value` VARCHAR(255) NULL, 
                  `time` int(10) not null default '0', 
                  PRIMARY KEY  (`id`), 
                  KEY `time` (`time`) 
                ) ENGINE=MEMORY; 
                "; 
        } 
        return $this->execute($sql,true); 
    } 
    private function execute($sql,$die=false) 
    { 
        if($die) 
        { 
            mysql_query($sql,$this->conn) or die("exe Sql error:<br>".mysql_error()."<br>".$sql."<hr>");            
        } 
        else 
        { 
            mysql_query($sql,$this->conn); 
            if(mysql_error()){ 
                return false; 
            }else{ 
                return true; 
            } 
        } 
    } 
    private function getOne($sql,$die=false){ 
        $rs = $this->query($sql,$die); 
        if($rs && ($one = mysql_fetch_row($rs)) ){ 
            return $one[0]; 
        }else{ 
            return false; 
        } 
    } 
    private function query($sql,$die=false){ 
        if($die) 
            $rs = mysql_query($sql,$this->conn) or die("query Sql error:<br>".mysql_error()."<br>".$sql."<hr>"); 
        else 
            $rs = mysql_query($sql,$this->conn); 
        return $rs; 
    } 

$lnk = mysql_connect('localhost', 'root', '123456') 
       or die ('Not connected : ' . mysql_error()); 
 
// make foo the current db 
mysql_select_db('test', $lnk) or die ('Can/'t use foo : ' . mysql_error()); 
$S = new session($lnk); 
if(!$S->last){ 
 $S->last = time(); 

echo "First visit at ".$S->last."<br>"; 
if(!$S->lastv){ 
 $S->lastv = 0; 

$S->lastv++; 
echo "lastv=".$S->lastv."<br>"; 
echo "test=".$S->test."<br>"; 
if(isset($_GET['max'])){ 
 $S->boom = str_repeat("OK",255);  

if(isset($_GET['boom'])){ 
 $S->boom = $_GET['boom'];  

echo "boom=".$S->boom."<br>"; 
?> 

    
[3]php在某年月日之后过期(不受服务器本身时间设置影响)的代码分享
    来源: 互联网  发布时间: 2013-12-24

代码如下:

<?php
/**
* php在指定时间内过期
* edit by www.
*/
while(! ($dataFile = @fopen('http://www./6892.html', "r" )) ){
 sleep(2);
}
//$dataFile = fopen('data.txt', "r" ) ;
$buffer = '';
if ( $dataFile )
{
 $buffer = "";
 while (!feof($dataFile)) 
 {
  $buffer .= fgets($dataFile, 4096);
 }
 //$buffer = iconv('gb2312','utf-8',$buffer);
 fclose($dataFile);
 $pos1 = strpos($buffer,'最后发表'); 
 $text  = substr($buffer,$pos1,1300);
 $pos1 = strpos($text,'<span >');
 $text  = substr($text,$pos1+strlen('<span >'));
 $text  = substr($text,0,strpos($text,']'));
 
 //$text = substr($buffer,$pos1,24*2-3);[2008-05-16 11:07]
 $date = preg_replace("/^/[(/d+)-(/d+)-(/d+)/s+(/d+):(/d+)$/","/$1-/$2-/$3 /$4:/$5",$text).date(":s");
 if(strtotime($date)>mktime(12,12,12,5,15,2008)){
  header("HTTP/1.0 501 Server Error");
  exit('<title>HTTP 501 - Server Internal Error</title>Server Internal Error.');
 }else{
  echo '<hr>';
 } 
}else{
 exit('server is down!');
}
?>

    
最新技术文章:
▪PHP函数microtime()时间戳的定义与用法
▪PHP单一入口之apache配置内容
▪PHP数组排序方法总结(收藏)
▪php数组排序方法大全(脚本学堂整理奉献)
▪php数组排序的几个函数(附实例)
▪php二维数组排序(实例)
▪php根据键值对二维数组排序的小例子
▪php验证码(附截图)
▪php数组长度的获取方法(三个实例)
▪php获取数组长度的方法举例
▪判断php数组维度(php数组长度)的方法
▪php获取图片的exif信息的示例代码
▪PHP 数组key长度对性能的影响实例分析
▪php函数指定默认值的方法示例
▪php提交表单到当前页面、提交表单后页面重定...
▪php四舍五入的三种实现方法
▪php获得数组长度(元素个数)的方法
▪php日期函数的简单示例代码
▪php数学函数的简单示例代码
▪php字符串函数的简单示例代码
▪php文件下载代码(多浏览器兼容、支持中文文...
▪php实现文件下载、支持中文文件名的示例代码...
▪php文件下载(防止中文文件名乱码)的示例代码
▪解决PHP文件下载时中文文件名乱码的问题
▪php数组去重(一维、二维数组去重)的简单示例
▪php小数点后取两位的三种实现方法
▪php Redis 队列服务的简单示例
▪PHP导出excel时数字变为科学计数的解决方法
▪PHP数组根据值获取Key的简单示例
▪php数组去重的函数代码示例
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3