php move_uploaded_file 中文乱码问题或上传失败问题

 更新时间:2016年11月25日 15:57  点击:2357

php是外国人做的,在中文支持上有些地方做得不完美,就在我们文件上传时就发现了,上传中文竟然不成功,
下面我们来看看实例与解决方法吧。
*/
$filepath ='upfile/';

if ($_FILES['file']['error']==0)
{
    
 if( move_uploaded_file($_FILES['file']['tmp_name'],$filepath.$_FILES['file']['name']))
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}


/*
.....
上面是一段简单的文件上传代码,传英语的是没有问题,但是中文的就不会成功了,好了,那我们如何处理中文文件名上传失败问题呢。
方法很简单就是对上传的文件名重命名就OK了,下面我们把程序修改一下。如下。
*/

if ($_FILES['file']['error']==0){
   
    $ext = end(explode('.',$_FILES['file']['name']));
 $fileRandName = time(); //根据当前时间生成一个字符串
 
 if( move_uploaded_file($_FILES['file']['tmp_name'],$filepath.$fileRandName.'.'.$ext))
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}

//这样不管你上传的是什么文件都OK了。
//本站原创文章转载注明来自www.111cn.net

<?php教程
//全国,判断条件是$REQUEST_URI是否含有html
if (!strpos($_SERVER["REQUEST_URI"],".html"))
{
$page="http://qq.ip138.com/weather/";
$html = file_get_contents($page,'r');
$pattern="/<B>全国主要城市、县当天和未来五天天气趋势预报在线查询</B>(.*?)<center style="padding:3px">/si";
//正则匹配之间的html
preg_match($pattern,$html,$pg);
echo "";
//正则替换远程地址为本地地址
$p=preg_replace('//weather/(w+)/index.htm/', 'tq.php/$1.html', $pg[1]);
echo $p;
}
//省,判断条件是$REQUEST_URI是否含有?
else if(!strpos($_SERVER["REQUEST_URI"],"?")){
//yoyo推荐的使用分割获得数据,这里是获得省份名称
$province=explode("/",$_SERVER["REQUEST_URI"]);
$province=explode(".",$province[count($province)-1]);
$province=$province[0];
//被注释掉的是我自己写出来的正则,感觉写的不好,但效果等同上面
//preg_match('/[^/]+[.(html)]$/',$_SERVER["REQUEST_URI"],$pro);
//$province=preg_replace('/.html/','',$pro[0]);
$page="http://qq.ip138.com/weather/".$province."/index.htm";
//获取html数据之前先尝试打开页面,防止恶意输入地址导致出错
if (!@fopen($page, "r")) {
die("对不起,该地址不存在!<a href=javascript教程:history.back(1)>点击这里返回</a>");
exit(0);
}
$html = file_get_contents($page,'r');
$pattern="/五天天气趋势预报</B>(.*?)请输入输入市/si";
preg_match($pattern,$html,$pg);
echo "";
//正则替换,获取省份,城市
$p=preg_replace('//weather/(w+)/(w+).htm/', '$2.html?pro=$1', $pg[1]);
echo $p;
}
else {
//市,通过get传递省份
$pro=$_REQUEST['pro'];
$city=explode("/",$_SERVER["REQUEST_URI"]);
$city=explode(".",$city[count($city)-1]);
$city=$city[0];
//preg_match('/[^/]+[.(html)]+[?]/',$_SERVER["REQUEST_URI"],$cit);
//$city=preg_replace('/.html?/','',$cit[0]);
$page="http://qq.ip138.com/weather/".$pro."/".$city.".htm";
if (!@fopen($page, "r")) {
die("对不起,该地址不存在!<a href=javascript:history.back(1)>点击这里返回</a>");
exit(0);
}
$html = file_get_contents($page,'r');
$pattern="/五天天气趋势预报</B>(.*?)请输入输入市/si";
preg_match($pattern,$html,$pg);
echo "";
//获取真实的图片地址
$p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]);
echo $p;
}
?>

 $time = '2010-1-11';
 
 //定义一个日间我相把把它变成2010-01-1
 
 $splitDate = explode("-",$time);
 
 //进行拆分以"-"分开
 
 $stime = mktime(0,0,0,$splitDate[1],$splitDate[2],$splitDate[0]);
 
 //再用mktime把它转换成时间载
 
 if( intval( $splitDate[1] )<10 && substr( $splitDate[1],0,1) !='0' )
 {
  $splitDate[1] = '0'.$splitDate[1];
 }
 //对月分取一个数字判断如果是01这种格式就不操作反之就加个0
 
 if( intval( $splitDate[2] )<10 && substr( $splitDate[2],0,1) !='0' )
 {
  $splitDate[2] = '0'.$splitDate[2];
 }
 //对日期作同样的作了,小于10就补0
 
 /*
  函数分析:
   explode 使用一个字符串分割另一个字符串 array explode ( string separator, string string [, int limit] )
   <a href=/phper/21/101d7c9a91356a428c8039c03dd4500b.htm>mktime</a>  函数返回一个日期的 Unix 时间戳。 mktime(hour,minute,second,month,day,year,is_dst)
   <a href=/phper/18/9351c693420d88336920eb2c12fca245.htm>intval</a> int intval ( mixed var [, int base])
通过使用特定的进制转换(默认是十进制),返回变量 var 的 integer 数值。

   substr  函数返回字符串的一部分 substr(string,start,length)
   
 */
 ?>

本站原创教程,转载注明来自www.111cn.net


/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

//close the zip -- done!
$zip->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
/***** Example Usage ***/
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
create_zip($files, 'myzipfile.zip', true);


17. PHP解压缩 Zip 文件


/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file($file, $destination){
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
echo 'Archive extracted to directory';
}

function destroyDir($dir, $virtual = false)
{
$ds = DIRECTORY_SEPARATOR;
$dir = $virtual ? realpath($dir) : $dir;
$dir = substr($dir, -1) == $ds ? substr($dir, 0, -1) : $dir;
if (is_dir($dir) && $handle = opendir($dir))
{
while ($file = readdir($handle))
{
if ($file == '.' || $file == '..')
{
continue;
}
elseif (is_dir($dir.$ds.$file))
{
destroyDir($dir.$ds.$file);
}
else
{
unlink($dir.$ds.$file);
}
}
closedir($handle);
rmdir($dir);
return true;
}
else
{
return false;
}
}

 


function list_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." && $file != ".." && $file != "Thumbs.db")
{
echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'." ";
}
}
closedir($handle);
}
}
}

[!--infotagslink--]

相关文章

  • php生成二维码中文乱码问题解决方法

    最近做了个扫描二维码得到vcard的项目,遇到一个问题,有一部分生成完的二维码,用android系统手机扫描后得到的vcard中的中文姓名是乱码,经过比对发现,这部分vcard中ORG这个...2016-11-25
  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • 运行vbs脚本报错无效字符、中文乱码的解决方法(编码问题)

    今天在写一个vbs的时候,发现中文乱码,后来写好代码正常运行的代码压缩一下给了同事,发现报无效字符,经过验证后发现原来是编码的问题导致,这里就为大家分享一下...2020-06-30
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • PHP json_encode() 函数详解及中文乱码问题

    在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。这个函数的功能是将数值转换成json数据存储格式。<&#63;php$arr = array ( 'Name'=>'希亚', 'Age'...2015-11-08
  • Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16
  • 解决HttpPost+json请求---服务器中文乱码及其他问题

    这篇文章主要介绍了解决HttpPost+json请求---服务器中文乱码及其他问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-22
  • php中iconv编码转换来解决中文乱码的问题

    用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些 代码如下 复制代码 ...2016-11-25
  • php mail发邮件标题中文乱码的问题解决办法

    本文章来给大家介绍php mail发邮件标题中文乱码的问题解决办法,希望到此类问题的朋友可进入参考。 当使用下面的PHP语句发送电子邮件的时候,如果编码和接收邮箱编码...2016-11-25
  • FlashFXP连接站点中文显示乱码解决办法

    FlashFXP是一款常用的服务器客户连接软件了,我们可以通过FlashFXP来上传或下载文件,但有一些朋友使用FlashFXP时碰到中文目录或文件名乱码问题,那么要如何来解决呢?具体就...2016-10-10
  • php中把unicode编码转化为中文

    小编在网上看到最多的就是汉字转换unicode编码了,今天我们看到一个反过来的操作就是把unicode转换成中文了,下面一起来看看 这两天帮别人开发微信平台好友板块,存...2016-11-25
  • php 判断是否是中文/英文/数字示例代码

    复制代码 代码如下: $str='asb天水市12'; if (preg_match("/^[/x7f-/xff]+$/", $str)){ echo '全部是汉字'; }else { echo '不全是汉字'; } /** PHP自带的判断是否是中文, eregi('[^/x00-/x7F]', $str ) //中文 ereg...2013-10-04
  • python os.listdir()乱码解决方案

    在本篇文章里小编给大家整理的是一篇关于python os.listdir()乱码解决方案,对此有兴趣的朋友们可以学习下。...2021-01-31
  • php中文转换成拼音代码

    <?php教程 function cn2pinyin($_string, $_code='gb2312') { $_datakey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|b...2016-11-25
  • JavaScript过滤字符串中的中文与空格方法汇总

    这篇文章主要介绍了JavaScript过滤字符串中的中文与空格方法汇总 的相关资料,需要的朋友可以参考下...2016-03-09