我正在尝试使用php上传我的文件。但是,不知道为什么它不起作用。请帮忙。
我的代码:
<?php
error_reporting(0)
$name = $_FILES["file"]["name"];
$tmp_name = $_FILES["file"]["tmp_name"];
if(isset($name))
{
if(!empty($name))
{
echo "OK <br>";
$location = "uploads/";
if(move_uploaded_file($tmp_name, $location))
{
echo "uploaded";
}
else
{
echo "file not uploaded";
}
}
else
{
echo "Please select a file";
}
}
?>
HTML code:
<form action="<?php $PHP_SELF ?>" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="SUBMIT!">
</form>
答案 0 :(得分:1)
您需要指定move_uploaded_file()
的完整路径。此外,is_uploaded_file()
对于前两个if()
语句就足够了。
按如下方式更新您的代码:
$file = $_FILES['file'];
if( is_uploaded_file($file['tmp_name']) )
{
$destination = 'uploads/'.$file['name'];
echo (move_uploaded_file($file['tmp_name'], $destination)) ? 'uploaded'
: 'file not uploaded';
}
else
{
echo 'Please select a valid file.';
}
请确保在uploads/
目录中设置相关权限。
答案 1 :(得分:0)
您必须设置目标文件名
$name = $_FILES["file"]["name"];
$tmp_name = $_FILES["file"]["tmp_name"];
if(isset($name))
{
if(!empty($name))
{
echo "OK <br>";
$location = "uploads/".$name;
if(move_uploaded_file($tmp_name, $location))
{
echo "uploaded";
}
else
{
echo "file not uploaded";
}
}
else
{
echo "Please select a file";
}
}
?>