'<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?> '
我已经尝试过application / pdf ....如何上传pdfs?我可以在我的网站上上传jpg和png,但我不能用于doc,txt或pdf。我知道这段代码是$ allowedExts = array(“jpg”,“jpeg”,“gif”,“png”);只是,但如何更改|| ($ _FILES [“file”] [“type”] ==“image / jpeg”)到|| ($ _FILES [“file”] [“type”] ==“application / pdf”)? Thanx提前...
答案 0 :(得分:3)
您的代码正在寻找扩展程序(pdf)&amp;&amp;类型
pdfs的mime类型:
application / pdf,application / x-pdf,application / acrobat,applications / vnd.pdf,text / pdf,text / x-pdf
Microsoft文档的扩展名和MIME类型: http://filext.com/faq/office_mime_types.php
我认为你可以解决这个问题:
修改添加此行的页面
echo "Type: " . $_FILES["file"]["type"] . "<br> Extension: " . $_FILES["file"]["name"] . "<br>";
它将显示文件类型和名称。