我已尝试过多次尝试此代码,我知道HTML是正确的,但PHP很棘手
<html>
<body>
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
move_uploaded_file($_FILES["file"]["tmp_name"],
$uploadfile);
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
?>
<h1>File upload successful!</h1>
<form method="get" action="/megan">
<input type="submit" value="Continue"/>
</form>
</body>
</html>
帮我解决这个问题
答案 0 :(得分:0)
嘿尝试这个简单的代码,我认为这会对您有所帮助,并根据您的要求添加代码。
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
<?php
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
$dir="var/www/Megan/";
move_uploaded_file($temp,$dir.$name);
?>
答案 1 :(得分:0)
首先,您认为HTML正确是错误的。文件上传表单需要是method =“POST”
您的一个表单元素需要是文件选择器:
<input type="file" name="file">
您的初始表单标记需要一个额外的参数:
<form method="post" enctype="multipart/form-data">
您的PHP只应在表单已POSTED时运行,在这种情况下设置变量$ _POST。所以把你的PHP代码放在
中if($_POST) {
//php here
}
答案 2 :(得分:0)
可能是您的上传目录是readonly或写保护
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
$uploaddir = '/var/www/Megan/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
chmod($uploadfile, 0777);
if($_FILES['file']['error']==0) {
if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile)){
echo "Stored in: " . "$uploaddir" . $_FILES["file"]["name"];
} else {
echo "error!!";
}
} else {
echo "An error has occurred.<br/>Error Code: " . $_FILES["file"]["error"];
}
?>