当前位置: 编程技术>php
本页文章导读:
▪php中md5与uniqid函数的例子 1,md5的例子
<?
$id = md5(uniqid(rand()));
echo "$id";
?>
2,使用md5函数加密文本
<?php
$val = "secret";
echo "Pre-hash string: $val <br />";
$hash_val = md5 ($val);
echo "Hashed outcome: $hash_val";
?.........
▪file_exists不支持中文文件名的解决方法 在使用file_exists()判断某个文件或文件夹是否存在时,如果文件或文件夹存在则返回true,否则返回false。
不过,该函数对于中文文件名或文件夹名不能返回正确值,始终返回false。
我想到.........
▪php读取excel的实例代码 之前我们介绍了php写excel文件的实现代码,今天我们介绍使用PHPExcel读取excel文件,读取出的文件包含标题栏等信息的方法。
注意:单元格第一行以1开始,第一列以0开始
代码:
<?php
/**
.........
[1]php中md5与uniqid函数的例子
来源: 互联网 发布时间: 2013-12-24
1,md5的例子
<? $id = md5(uniqid(rand())); echo "$id"; ?>
2,使用md5函数加密文本
<?php $val = "secret"; echo "Pre-hash string: $val <br />"; $hash_val = md5 ($val); echo "Hashed outcome: $hash_val"; ?>
3,用uniqid函数生成id
<?
$id = uniqid("phpuser");
echo "$id";
?>
[2]file_exists不支持中文文件名的解决方法
来源: 互联网 发布时间: 2013-12-24
在使用file_exists()判断某个文件或文件夹是否存在时,如果文件或文件夹存在则返回true,否则返回false。
不过,该函数对于中文文件名或文件夹名不能返回正确值,始终返回false。
我想到了一个解决方法,分享一下。
代码:
<?php
//解决file_exists不支持中文文件名的问题
$filename = iconv("UTF-8","GB2312",$filename);
if (!file_exists($filename)) {
return false;
}
//经过如上的转换,则对中英文就都可以支持了
?>
[3]php读取excel的实例代码
来源: 互联网 发布时间: 2013-12-24
之前我们介绍了php写excel文件的实现代码,今天我们介绍使用PHPExcel读取excel文件,读取出的文件包含标题栏等信息的方法。
注意:单元格第一行以1开始,第一列以0开始
代码:
<?php
/**
* php 读取excel文件
* edit by www.
*/
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
class ExcelHelper{
public function importFileContacts($filename){
try {
//解决文件中文名问题
$filename = iconv("UTF-8","GB2312",$filename);
if (!file_exists($filename)) {
return false;
}
//chmod($filename, 0750);linux下改变文件权限
$filetype = $this->getFileType($filename);
//根据文件类型读取excel文件
if ($filetype == "xlsx") {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
} elseif ($filetype == "xls") {
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
} else {
return false;
}
$mems = NULL;
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($row = 1; $row <= $highestRow; $row++) {
for ($col = 0; $col < $highestColumnIndex; $col++) {
$mem[$col] = trim($objWorksheet->getCellByColumnAndRow($col, $row)->getValue());
}
$mems[$row - 1] = $mem;
}
return $mems;
} catch (Exception $e) {
echo 'EXCEL ERROR:' . $e->getMessage();
$errText = "Read excel error:Please retry later!";
return $errText;
}
}
/**
* 获取文件类型
* @param $filenamePath 文件路径或者文件名
*/
private function getFileType($filenamePath){
if (!$filenamePath){
return false;
}
$filenameArr = explode('/', $filenamePath);
$count = count($filenameArr);
$filename = $filenameArr[$count-1];
$filetypeArr = explode('.', $filename);
$count = count($filetypeArr);
$filetype = $filetypeArr[$count-1];
return $filetype;
}
}
?>最新技术文章: