功能:实现文件上传:
文件:index2.html upload.php
[root@localhost htdocs]# cat index2.html
文件上传实例
上传新文件
=================================================
[root@localhost htdocs]# cat upload.php
上传文件...上传文件...
if ($_FILES['userfile']['error'] > 0)
{
echo '上传错误: ';
switch ($_FILES['userfile']['error'])
{
case 1: echo '上传文件大小超出了PHP配置文件中的约定值:upload_max_filesize'; break;
case 2: echo '上传文件大小超出了表单中的约定值: max_file_size'; break;
case 3: echo '文件只被部分上载'; break;
case 4: echo '没有上传任何文件'; break;
}
exit;
}
// Does the file have the right MIME type?
if ($_FILES['userfile']['type'] != 'text/plain')
{
echo '问题: 文件不是一个文本文件。';
exit;
}
// put the file where we'd like it
$upfile = $_FILES['userfile']['name'];
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) //判断是否为上传文件
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))//移动文件
{
echo '问题: 不能将文件移动到指定目录。';
exit;
}
}
else
{
echo '问题: 上传文件不是一个合法文件: ';
echo $_FILES['userfile']['name'];
exit;
}
echo 'File uploaded successfully
';
// reformat the file contents
$fp = fopen($upfile, 'r');
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, 'w');
fwrite($fp, $contents);
fclose($fp);
// show what was uploaded
echo 'Preview of uploaded file contents:
';
echo $contents;
echo '
';
?>