我正在尝试将文件(pdfs)上传并存储在某个目录中
我正在使用move_uploaded_file
:
move_uploaded_file($_FILES["userdata"]["tmp_name"],
"uploads/data/" . $_FILES["userdata"]["name"]);
脚本运行但是当我通过FTP检查目录时,没有任何内容。
此目录及其父目录的权限为777
。
我在HTML表单中使用enctype="multipart/form-data"
。
有人知道我做错了吗?
var_dump($_FILES);
给出:
array(2) { ["userdata"]=> array(5) { ["name"]=> string(22) "myfile.pdf" ["type"]=> string(15) "application/pdf" ["tmp_name"]=> string(14) "/tmp/phpiUJVu7" ["error"]=> int(0) ["size"]=> int(1398957) } ["userdata2"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }
我的完整代码是:
$allowedExts = array("pdf", "doc", "docx", "wps", "odt");
$temp = explode(".", $_FILES["userdata"]["name"]);
$extension = end($temp);
if ((($_FILES["userdata"]["type"] == "application/pdf")
|| ($_FILES["userdata"]["type"] == "application/msword")
|| ($_FILES["userdata"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["userdata"]["type"] == "application/vnd.ms-works")
|| ($_FILES["userdata"]["type"] == "iapplication/vnd.oasis.opendocument.text"))
&& ($_FILES["userdata"]["size"] < 4000000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["userdata"]["error"] > 0) {
echo "Return Code: " . $_FILES["userdata"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["userdata"]["name"] . "<br>";
echo "Type: " . $_FILES["userdata"]["type"] . "<br>";
echo "Size: " . ($_FILES["userdata"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["userdata"]["tmp_name"] . "<br>";
if (file_exists("upload/data/" . $_FILES["userdata"]["name"])) {
echo $_FILES["userdata"]["name"] . " already exists. ";
}
else {
move_uploaded_file($_FILES["userdata"]["tmp_name"],
"uploads/data/" . $_FILES["userdata"]["name"]);
echo "Stored in: " . "upload/data/" . $_FILES["userdata"]["name"];
}
}
}
else {
echo "Invalid file";
}
所有这些if语句都通过了,我没有收到任何错误。
答案 0 :(得分:1)
您的var_dump表示input
html中的文件名为coverletter
和resume
,但您尝试以$_FILES["userdata"]
进行访问。尝试改变它,它可能会有所帮助
更新:
uploads
是move_uploaded_file()
同时尝试像这样检查move_uploaded_file
if (move_uploaded_file($_FILES["userdata"]["tmp_name"],"upload/data/" . $_FILES["userdata"]["name"]);) {
echo "Stored in: " . "upload/data/" . $_FILES["userdata"]["name"];
} else {
echo "Move upload seems to be struct !" ;
}