大家好,我写了一个上传程序。文本文件上传之后再显示其内容。
上传过程没有问题,但在最后显示文本文件的内容时,中文内容出现乱码,英文就没有问题。
我想问下这是什么原因?需要怎么解决?谢谢大家!
代码如下:
首页代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head><body>
<p>Upload new news files.</p>
<form id="form1" name="form1" method="post" action="31.php" enctype="multipart/form-data">
upload a file:
<label>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"/>
<input type="file" name="userfile" />
</label>
<label>
<input type="submit" name="Submit" value="Send" />
</label>
</form><p> </p>
</body>
</html>上传代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head><body>
<?php
include ('conn.php');
?>
<?php
$userfile=$_FILES['userfile'];
switch ($userfile['error'])
{
case 1: echo 'File exceeded upload_max_filesize';
break;
case 2: echo 'File exceeded max_file_size';
break;
case 3: echo 'File only partially uploaded';
break;
case 4: echo 'No file uploaded';
break;
case 6: echo 'Cannot upload file:No temp directory specified';
break;
case 7: echo 'Upload failed:Cannot write to disk';
break;
}
if ($userfile['type'] !='text/plain')
{
echo 'Error, I am sorry that the file is not plain text.';
exit;
}
$upfile='./up/'.$userfile['name'];
if (is_uploaded_file($userfile['tmp_name']))
{ if (!move_uploaded_file($userfile['tmp_name'],$upfile))
{
echo 'Error,Could not move file to destination directory.';
exit;
} else
echo 'File uploaded successfully';
}
else
{
echo 'Error, No file is uploaded!';
exit;
}
$contents= file_get_contents($upfile);$contents=strip_tags($contents);
file_put_contents($userfile['name'],$contents);
echo '<p> Previes of uploaded file contents:<br/><hr/>';echo nl2br($contents);echo '<br/><hr/>';
$db->close();
?>
</body>
</html>
谢谢大家给予详细的指导!
上传过程没有问题,但在最后显示文本文件的内容时,中文内容出现乱码,英文就没有问题。
我想问下这是什么原因?需要怎么解决?谢谢大家!
代码如下:
首页代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head><body>
<p>Upload new news files.</p>
<form id="form1" name="form1" method="post" action="31.php" enctype="multipart/form-data">
upload a file:
<label>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000"/>
<input type="file" name="userfile" />
</label>
<label>
<input type="submit" name="Submit" value="Send" />
</label>
</form><p> </p>
</body>
</html>上传代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head><body>
<?php
include ('conn.php');
?>
<?php
$userfile=$_FILES['userfile'];
switch ($userfile['error'])
{
case 1: echo 'File exceeded upload_max_filesize';
break;
case 2: echo 'File exceeded max_file_size';
break;
case 3: echo 'File only partially uploaded';
break;
case 4: echo 'No file uploaded';
break;
case 6: echo 'Cannot upload file:No temp directory specified';
break;
case 7: echo 'Upload failed:Cannot write to disk';
break;
}
if ($userfile['type'] !='text/plain')
{
echo 'Error, I am sorry that the file is not plain text.';
exit;
}
$upfile='./up/'.$userfile['name'];
if (is_uploaded_file($userfile['tmp_name']))
{ if (!move_uploaded_file($userfile['tmp_name'],$upfile))
{
echo 'Error,Could not move file to destination directory.';
exit;
} else
echo 'File uploaded successfully';
}
else
{
echo 'Error, No file is uploaded!';
exit;
}
$contents= file_get_contents($upfile);$contents=strip_tags($contents);
file_put_contents($userfile['name'],$contents);
echo '<p> Previes of uploaded file contents:<br/><hr/>';echo nl2br($contents);echo '<br/><hr/>';
$db->close();
?>
</body>
</html>
谢谢大家给予详细的指导!
解决方案 »
- 这是什么类型的数据?有没系统函数拆分的?
- php页面调用js代码中的参数,在线等
- 数据库共有5000多条数据,每30条插入一个时间(共有180个时间) 怎么写php或者mysql的语句?
- smarty 模板循环生成的问题
- 请教上海的同行
- 求助:session变量不能传递
- Php 从SQL Server读取日期字段的显示问题,月份老是是中文。其它是数字。
- 关于PHP运行环境及配置的问题?
- 用phpinfo()如何查看是否支持mysql
- 怎么写这样的语句??
- 江湖求救:小弟安装LAMP平台遇到一点问题:Fatal error: Class 'mysqli' not found
- odbc_fetch_array如何获取数字数组?
要保证内容编码与页面编码一致
现在页面是utf8的,如果上传utf8文件,查看应该没问题,如果上传gbk的文件,需要先转换成utf8再显示
看看IE 的编码是否正确