<?php
$name = $_FILES["file"]["name"];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset ($name)) {
if (!empty($name)) {
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded';
}
} else {
echo 'please choose a file';
}
}
?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
我收到’通知:未定义的索引’错误消息. enctype包含在表单标签中,所以我无法弄清楚它是什么..任何人都可以帮助我吗?
解决方法:
如果没有上传任何内容并且isset测试有点无用,则第一个赋值会抛出警告.
您可以按如下方式更改代码
<?php
if (isset($_FILES["file"]["name"])) {
$name = $_FILES["file"]["name"];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (!empty($name)) {
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded';
}
} else {
echo 'please choose a file';
}
}
?>
<form action="test.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>