首页 > 编程技术 > php

php sys_get_temp_dir -返回临时文件路径

发布时间:2016-11-25 16:39

好了费话不用说多了我们来看看这款php sys_get_temp_dir -返回临时文件路径函数使用方法与实现原理吧。

sys_get_temp_dir
( PHP 5中“ = 5.2.1 )

sys_get_temp_dir -返回目录路径用于临时文件

描述
字符串sys_get_temp_dir (无效)
返回目录路径的PHP商店临时文件在默认情况下。

返回值
返回路径的临时目录中。

实例

例如# 1 sys_get_temp_dir ( )的例子

Examples

Example #1 sys_get_temp_dir() example

<?php
// Create a temporary file in the temporary
// files directory using sys_get_temp_dir()
$temp_file = tempnam(sys_get_temp_dir(), 'Tux');

echo $temp_file;
?>
The above example will output something similar to:

C:WindowsTempTuxA318.tmp

此函数的实现方法:

<?php
if ( !function_exists('sys_get_temp_dir')) {
  function sys_get_temp_dir() {
    if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
    if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
    if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
    $tempfile=tempnam(uniqid(rand(),TRUE),'');
    if (file_exists($tempfile)) {
    unlink($tempfile);
    return realpath(dirname($tempfile));
    }
  }
}
?>

class DB
{
 //database connection
 var $con = FALSE;

 function DB($MYSQL_HOST=MYSQL_HOST, $MYSQL_USER=MYSQL_USER, $MYSQL_PASS=MYSQL_PASS,$MYSQL_DB=MYSQL_DB)
 {
  $this->con = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS) or die("Could not connect to database");

  if ($this->con)
  {
   @mysql_select_db($MYSQL_DB, $this->con) or die ("Could not select database");
  }

  return $this->con;
 }


 function Query($sql, $tran = false)
 {
 // if (!file_exists(MYSQL_LOG))
 // {
 //  @umask(0);
 //  @mkdir(MYSQL_LOG, 0777);
 // }

  // ¼־
  //$fp = @fopen(MYSQL_LOG.date("Ymd").".txt", "a");

  // д־
 // @fwrite($fp, date("Y-m-d H:i:s")."|$sql ");
 // @fclose($fp);

  $this->sql = $sql;

  if ($tran)
  {
   $this->result = @mysql_query($this->sql) OR $this->RollBack();
   return $this->result;
  }
  else
  {
      mysql_query("SET NAMES 'utf8'");
   //mysql_query("SET NAMES 'gbk'");
   $this->result = @mysql_query($this->sql);
   return $this->result;
  }
 }


 function RollBack()
 {
  $this->Query("ROLLBACK;");
  die("MySQL ROLLBACK;");
 }


 function NumRows($result)
 {
  $this->result = $result;
  return @mysql_num_rows($this->result);
 }


 function FetchRow($result)
 {
  $this->result = $result;
  return @mysql_fetch_row($this->result);
 }


 function FetchArray($result)
 {
  $this->result = $result;
  return @mysql_fetch_array($this->result, MYSQL_ASSOC);
 }
 function FetchArray2($result)
 {
  $this->result = $result;
  return @mysql_fetch_array($this->result, MYSQL_BOTH);
 }
 

 function FetchObject($result)
 {
  $this->result = $result;
  return @mysql_fetch_object($this->result);
 }


 function FreeResult($result)
 {
  $this->result = $result;
  return @mysql_free_result($this->result);
 }

 function DataSeek($result)
 {
 //复位记录集指针
  $this->result = $result;
  return mysql_data_seek($this->result,0);
 }
 
 function InsertID()
 {
  //$this->con = $con;
  return @mysql_insert_id($this->con);
 }


 function Close()
 {
  if($this->con)
  {
   @mysql_close($this->con);
  }
 }
}

php urlencode 与 rawurlencode 教程
rawurlencode
( PHP 4中, PHP 5中)

rawurlencode -网址编码根据的RFC 1738

描述
字符串rawurlencode (字符串$str)
编码特定字符串根据»的RFC 1738 。

参数

str
将URL进行编码。


返回值
返回一个字符串中的所有非字母数字字符除了- _ 。被替换成一个百分号( % )符号后面跟两个十六进制数字。这是编码描述»的RFC 1738年为保护字面字符被解释为特殊的URL分隔和保护的URL被毁损的传输媒体转换与性质(如一些电子邮件系统) 。

实例

例如# 1包括一个密码的FTP网址

<?php
$a = explode('&', $QUERY_STRING);
$i = 0;
while ($i < count($a)) {
    $b = split('=', $a[$i]);
    echo 'Value for parameter ', htmlspecialchars(urldecode($b[0])),
         ' is ', htmlspecialchars(urldecode($b[1])), "<br />n";
    $i++;
}
?>

进行urlencode
( PHP 4中, PHP 5中)

进行urlencode -网址编码字符串

描述
字符串进行urlencode (字符串$str)
此功能方便的编码字符串被用于查询的网址的一部分,作为一种便捷的方式传递变量的下一页。

参数

str
字符串编码。


返回值
返回一个字符串中的所有非字母数字字符除了- _ 。被替换成一个百分号( % )符号后面跟两个十六进制数字和空格编码为加号( + )的迹象。这是相同的编码方式,从公布的数据编码的WWW形式,这是同样的方式在应用/的X WWW的形式了urlencoded的媒体类型。这不同于参考» RFC 1738编码(见rawurlencode ( ) )中,对历史的原因,空格编码为加号( + )的迹象。

实例

例如# 1进行urlencode ( )的例子

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>


Example #2 urlencode() and htmlentities() example

<?php
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>

flock在php中的作用是操作文件时锁定文件,只取取消锁定时才可用。

flock
( PHP 4中, PHP 5中)

flock-便携式咨询文件锁定

描述
布尔群(资源$处理,诠释$操作[摘要& $ wouldblock ] )
flock( )可让您执行一个简单的读卡器/写模式,它可以用在几乎所有平台(包括大多数Unix衍生物甚至Windows ) 。

被释放的锁也fclose ( ) (也被称为脚本时自动完成) 。

PHP支持便携式方式锁定在一个完整的档案咨询方式(也就是说所有访问程序必须使用同样的方式锁定或它不会工作) 。

参数

把柄
一个开放的文件指针。

操作
操作下列内容之一:

LOCK_SH获得共享锁(读者) 。
LOCK_EX获得独占锁定(作家) 。
LOCK_UN释放锁(共享或专用) 。
LOCK_NB如果你不想flock( )阻止,同时锁定。 (不支持在Windows上)

wouldblock
可选的第三个参数设置为TRUE ,如果锁定会阻止( EWOULDBLOCK errno条件) 。


返回值
返回TRUE或FALSE的成功失败。

修改

版本说明
4.0.1常量的LOCK_XXX增加了。在此之前,您必须使用1 LOCK_SH , 2 LOCK_EX , 3 LOCK_UN和4 LOCK_NB


实例

例如# 1flock( )的例子

<?php

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

if (flock($fp, LOCK_EX)) { // do an exclusive lock
    fwrite($fp, "Write something heren");
    flock($fp, LOCK_UN); // release the lock
} else {
    echo "Couldn't lock the file !";
}

fclose($fp);

?>

mysql_get_server_info

定义和用法
该mysql_get_server_info ( )函数得到有关MySQL服务器。

这个函数返回MySQL服务器版本上的成功,或FALSE的失败。

语法

mysql_get_server_info(connection)

参数说明
connection:

可选。指定MySQL连接。如果没有指定,最后连接开幕mysql_connect ( )或mysql_pconnect ( )的使用。

 

来看看实例.

 

<?php
$con = mysql_connect("localhost", "peter", "abc123");
echo "MySQL server info: " . mysql_get_server_info($con);
?>

 

输出信息为mysql信息哦.

标签:[!--infotagslink--]

您可能感兴趣的文章: