我正在尝试让php上传脚本工作。但我不确定为什么$_FILES看起来是空数组?有人能告诉我下面的剧本出了什么问题吗?
它假设将任意文件上传到/var/www/test/文件夹
<html>
<body>
<?php
if (isset($_POST['Submit'])) {
$target_path = "/var/www/test/";
$target_path = $target_path . basename($_FILES['myupload']['name']);
if(!move_uploaded_file($_FILES['myupload']['tmp_name'], $target_path)) {
echo '<pre>';
print 'The $_FILES content is :';
print_r($_FILES);
var_dump($_FILES);
echo '<pre>';
print 'the target path is:' . $target_path;
echo '<pre>';
print 'the $_POST variable content is :';
print_r($_POST) ;
echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';
} else {
echo '<pre>';
echo $target_path . ' succesfully uploaded!';
echo '</pre>';
}
}
?>
<form method="POST" action="" enctype"multipart/form-data">
Choose an image to upload:
<br>
<input type="file" name="myupload">
<br>
<br>
<input type="submit" value="Upload(This_is_just_button_name_display)" name="Submit">
</form>
</body>
</html>
不要丢弃$_FILES我从未丢弃过它,对我来说很好。试试这段代码,将php放在另一个文件中,并将表单重定向到它并添加此行在脚本的末尾
<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=samplehtml.html">
所以最后的代码是:
<?php
if (isset($_POST['Submit'])) {
$target_path = "/var/www/test/";
$target_path = $target_path . basename($_FILES['myupload']['name']);
if(!move_uploaded_file($_FILES['myupload']['tmp_name'], $target_path)) {
echo '<pre>';
print 'The $_FILES content is :';
print_r($_FILES)
echo '<pre>';
print 'the target path is:' . $target_path;
echo '<pre>';
print 'the $_POST variable content is :';
print_r($_POST) ;
echo '<pre>';
echo 'Your image was not uploaded.';
echo '</pre>';
} else {
echo '<pre>';
echo $target_path . ' succesfully uploaded!';
echo '</pre>';
}
}
?>
它现在应该可以工作了。