我正在尝试将文件上传到文件夹但无法上传。它给了我错误信息。
文件结构为:c:/xampp/htdocs/uploadfile/uploadfile.php
,文件夹为c:/xampp/htdocs/uploadfile/abc/
这是我的代码:
uploadfile.php
<form method="post" enctype="multipart/form-data" action="upload.php">
<input name="userfile" type="file" id="file" /><br />
<input type="submit" value="Upload Selected File" class="submit" />
</form>
upload.php的
<?php
$allowed = array('pdf','doc' ,'docx');
$filename = $_FILES['userfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'There is an Error Occured while Uploading. <br><br>Please select right file type and try again.';
}
else {
$uploaddir = '/abc/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo $uploadfile;
if (move_uploaded_file($_FILES['userfile']['name'], $uploaddir)) {
echo "<br><br>File is valid, and was successfully uploaded.\n";
} else {
echo "Error Occured while uploading file. <br><br>Please try later.";
}
}
?>
答案 0 :(得分:1)
move_uploaded_file()函数不正确。
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)
答案 1 :(得分:1)
尝试$_FILES['userfile']['tmp_name']
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
还尝试更改
$uploaddir = '/abc/';
到
$uploaddir = 'abc/';
或$uploaddir ='./uploadfile/abc/';
更多阅读手册: - http://in2.php.net/manual/en/function.move-uploaded-file.php
答案 2 :(得分:1)
Check the folder name is exist and check the permission of that particular folder
if(!in_array($ext,$allowed) ) {
echo 'There is an Error Occured while Uploading. <br><br>Please select right file type and try again.';
}
else {
$uploaddir = 'abc/';
// $uploadfile = basename($_FILES['userfile']['name']);
// echo $uploadfile;
if (move_uploaded_file($_FILES['userfile']['name'], $uploaddir)) {
echo "<br><br>File is valid, and was successfully uploaded.\n";
} else {
echo "Error Occured while uploading file. <br><br>Please try later.";
}
}
答案 3 :(得分:0)
请检查:
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)
答案 4 :(得分:0)
你应该使用
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {..}
因为PHP首先检查以确保filename指定的文件是有效上传文件(意味着它是通过PHP's
HTTP POST
上传机制上传并存储在临时文件中地点)。
如果文件有效,它将被移动到目的地给出的文件名。
注意:但是,仅限于目标路径上的限制,以允许移动文件名可能与此类限制冲突的上载文件。 move_uploaded_file()
只允许通过PHP上传的文件移动,从而确保此操作的安全性。
如果目标文件已存在,则会被覆盖。
此外,您还可以通过HTTP POST
功能is_uploaded_file()
检查文件是否已上传。