我想使用php上传文件(pdf),
我在互联网上找到了一个代码,我只是添加了我的文件类型:application / pdf 但它对pdf文件没有用。 (它适用于图像)。
html代码
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
php代码
$allowedExts = array("gif", "pdf", "GIF", "jpeg", "JPEG", "jpg", "JPG", "png", "PNG");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 90000000000) // increased allowed size may be your problem
&& 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";
}
错误
Invalid file
*
更新
*
在我的脚本开头添加var_dump($_FILES['file']);
在Chrome上(工作正常)输出为:
array
'name' => string 'Nouveau Document Microsoft Office Word.pdf' (length=42)
'type' => string 'application/pdf' (length=15)
'tmp_name' => string 'C:\wamp\tmp\php58AD.tmp' (length=23)
'error' => int 0
'size' => int 79770
Upload: Nouveau Document Microsoft Office Word.pdf
Type: application/pdf
Size: 77.900390625 kB
Temp file: C:\wamp\tmp\php58AD.tmp
array
'name' => string 'Nouveau Document Microsoft Office Word.pdf' (length=42)
'type' => string 'application/force-download' (length=26)
'tmp_name' => string 'C:\wamp\tmp\phpBEFD.tmp' (length=23)
'error' => int 0
'size' => int 79770
string 'application/force-download' (length=26)
UPDATE2
*代码wirthout $ _FILES [&#34; file&#34;] [&#34; type&#34;] *
var_dump($_FILES["file"]["type"]);
$allowedExts = array("gif","pdf", "GIF");
$extension = end(explode(".", $_FILES["file"]["name"]));
in_array($extension, $allowedExts);
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>";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
它适用于我的所有浏览器,但我必须测试文件的类型和大小!什么问题?
答案 0 :(得分:2)
请注意,文件mime类型是由客户端发送的,而不是由服务器检测到的。因此,它很容易被愚弄。
你不应该检查。删除任何关于它的检查,只需检查文件扩展名。
您可能想要更改此行:
$extension = end(explode(".", $_FILES["file"]["name"]));
有了这个:
$extension = strtolower(end(explode(".", $_FILES["file"]["name"])));
只需使用小写扩展名即可更改数组。