我正在尝试使用PHP将文件上传到服务器。我无法让它发挥作用。这是代码:
if( isset($_POST['Upload']) )
{
//size condition
if ( $_FILES['uploaded']['size'] > 350000)
{
$mesg = "Your file is too large.<br>";
exit;
}
if( move_uploaded_file($_FILES['uploaded']['tmp_name'], "upload/" . $_FILES['uploaded']['name'] ) )
{
$mesg = "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else
{
$mesg = "Sorry, there was a problem uploading your file.";
}
}
else
{
$mesg = "Select a File to upload.";
}
以下是我用来提交文件的表单的代码:
<?
echo $mesg;
?>
<br /><br />
<form enctype="multipart/form-data" action="" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
答案 0 :(得分:6)
您的enctype="multipart/form-data"
标记内需要<form>
,否则不会上传任何内容。
有关详情,请查看PHP manual。
此外,我不确定您是否只是这样做来测试功能,但您应该警惕将上传的文件放在可访问Web的文件夹中,尤其是使用其原始名称。这为有人上传恶意脚本并可能接管您的服务器敞开了大门。
答案 1 :(得分:3)
您的提交按钮没有名称:
<input type="submit" value="Upload" />
您正在检查$_POST['Upload']
,因此您可能需要:
<input type="submit" value="Upload" name="Upload" />
答案 2 :(得分:1)
将表单更改为
<form action="" method="POST" enctype="multipart/form-data">