PHP Upload form, PDF, Doc amp; Docx(PHP 上传表格、PDF、Doc amp;文档)
问题描述
我正在努力使这个上传代码适用于 docx 文件,它适用于 doc 和 pdf..
I'm struggling to make this upload code work for a docx file, it works okay for doc and pdf..
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
这是不久前的一个项目的一部分,老实说,我不记得该怎么做..
this is part of a project from a while ago and i honestly can't remember how to do it..
我知道这不是最安全的上传方法,但如果有人可以提供帮助,我们将不胜感激!
I know it's not the most secure upload method, but if someone could help it would be appreciated!
我想我需要在这里添加另一行:
I'm thinking i need to add another line here:
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
只是不确定是什么......感谢帮助!
Just not sure what.. Help is appreciated!
所以我已经到了这个阶段(在评论的帮助下!)
So i've got to this stage (with the help of comments!)
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
//if ((($_FILES["file"]["type"] == "application/pdf")
//|| ($_FILES["file"]["type"] == "application/msword"))
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats- officedocument.wordprocessingml.document"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
但现在它出现了:解析错误:语法错误,第 30 行/var/sites/s/stanation.com/public_html/forms/process/insert.php 中的意外 T_BOOLEAN_AND
But now its coming up with: Parse error: syntax error, unexpected T_BOOLEAN_AND in /var/sites/s/stanation.com/public_html/forms/process/insert.php on line 30
推荐答案
对于 docx
检查这个 MIME 类型
For docx
check this MIME type
application/vnd.openxmlformats-officedocument.wordprocessingml.document
这是代码.你缺少括号
<?php
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Success";
}
}
这篇关于PHP 上传表格、PDF、Doc &文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 上传表格、PDF、Doc &文档
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01