仅仅实现一个小的上传功能,并没有过多注意代码的质量
html
<html>
<head><title>test the php</title></head>
<body>
<h1> upload new file to website</h1>
<form action="test3.php" method="post" enctype="multipart/form-data">
<div>
<input type="hidden" name="MAX_SIZE" value="100000000"/>
<lable for="userfile" >upload a file :</lable>
<input type="file" name="userfile" id="userfile" />
<input type="submit" value="submit" />
</div>
</form>
</body>
</html>
test3.php
<?php
if($_FILES['userfile']['error'] > 0)
{
echo "upload error"."</br>";
exit;
}
else
{
echo "upload ok"."</br>";
}
if(file_exists("/var/www/".$_FILES['userfile']['name']))
{
echo "file already exists"."</br>";
exit;
}
else
{
move_uploaded_file($_FILES['userfile']['tmp_name'], "/var/www/".$_FILES['userfile']['name']); //必须修改上传文件名,否则脚本退出会自动删除上传文件
echo "upload finished<br>";
}
?>
注意:php.ini文件中有关上传的参数配置
file_uploads, upload_tmp_dir, upload_max_filesize, post_max_size
test.php
<html>
<head><title>test the php</title></head>
<body>
<h1> upload new file to website</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">
<div>
<input type="hidden" name="MAX_SIZE" value="100000000"/>
<lable for="userfile" >upload a file :</lable>
<input type="file" name="userfile" id="userfile" />
<input type="submit" value="submit" />
</div>
</form>
</body>
</html>
<?php
if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
{
if($_FILES['userfile']['error'] > 0)
{
echo "upload error"."</br>";
exit;
}
else
{
echo "upload ok"."</br>";
}
if(file_exists("/var/www/".$_FILES['userfile']['name']))
{
echo "file already exists"."</br>";
exit;
}
else
{
move_uploaded_file($_FILES['userfile']['tmp_name'], "/var/www/".$_FILES['userfile']['name']);
echo "upload finished<br>";
}
}
?>
利用HTTP/Upload方式:
<html>
<head><title>test the php</title></head>
<body>
<h1> upload new file to website</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post" enctype="multipart/form-data">
<div>
<input type="hidden" name="MAX_SIZE" value="100000000"/>
<lable for="userfile" >upload a file :</lable>
<input type="file" name="userfile" id="userfile" />
<input type="submit" value="submit" name="ok"/>
</div>
</form>
</body>
</html>
<?php
require('HTTP/Upload.php');
if(isset($_POST['ok']))
{
echo "test...<br>";
$upload = new HTTP_Upload();
$file = $upload->getFiles('userfile');
if($file == null)
{
echo "file == NULL <br>";
exit;
}
//print_r($file);
echo $_FILES['userfile']['name']."</br>";
if($file->isValid())
{
$file->moveTo('/var/www/');
echo "upload ok<br>";
}
else
{
echo $file->errorMsg();
}
}
?>