【发布时间】:2014-02-10 12:52:38
【问题描述】:
我一直在尝试各种方法将文件上传到 wordpress 网站上传目录(我现在给它 777 权限只是为了确保我没有遇到权限问题)。 我有一个放置代码的自定义 php 插件。
我尝试使用 wordpress 功能,但它们似乎不起作用。
所以现在我在 php 中得到了一个 sn-p 代码,它应该处理文件上传(通过表单提交)。
这是我的处理代码:
//image upload handling
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["editfile"]["name"]);
$extension = end($temp);
if ((($_FILES["editfile"]["type"] == "image/gif")
|| ($_FILES["editfile"]["type"] == "image/jpeg")
|| ($_FILES["editfile"]["type"] == "image/jpg")
|| ($_FILES["editfile"]["type"] == "image/pjpeg")
|| ($_FILES["editfile"]["type"] == "image/x-png")
|| ($_FILES["editfile"]["type"] == "image/png"))
&& ($_FILES["editfile"]["size"] < 5000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["editfile"]["error"] > 0)
{
$tim_core_edit_company_page .= "Return Code: " . $_FILES["editfile"]["error"] . "<br>";
}
else
{
$tim_core_edit_company_page .= "Upload: " . $_FILES["editfile"]["name"] . "<br>";
$tim_core_edit_company_page .= "Type: " . $_FILES["editfile"]["type"] . "<br>";
$tim_core_edit_company_page .= "Size: " . ($_FILES["editfile"]["size"] / 1024) . " kB<br>";
$tim_core_edit_company_page .= "Temp file: " . $_FILES["editfile"]["tmp_name"] . "<br>";
if (file_exists(wp_upload_dir() . $_FILES["editfile"]["name"]))
{
$tim_core_edit_company_page .= $_FILES["editfile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["editfile"]["tmp_name"],
"upload/" . $_FILES["editfile"]["name"]);
$tim_core_edit_company_page .= "Stored in: " . "upload/" . $_FILES["editfile"]["name"];
}
}
}
else
{
$tim_core_edit_company_page .= "Invalid file";
}
这是用于提交文件的表单:
<form action="' . site_url('/edit-company/?cid=' . $_GET['cid']
. '&lid=' . $_GET['lid']
. '&cname=' . $_GET['cname'] . '&editcompany=1&todb=1' ) . '" method="post">
<label><b>Company Name:</b></label> <input type="text" name="editname" value="' . $company->company_name . '">
<label><b>Address:</b></label> <input type="text" name="editaddress" value="' . $company->company_address . '">
<label><b>Telephone:</b></label> <input type="text" name="edittelephone" value="' . $company->company_telephone . '">
<label><b>Fax:</b></label> <input type="text" name="editfax" value="' . $company->company_fax . '">
<label><b>Email:</b></label> <input type="text" name="editemail" value="' . $company->company_email . '">
<label><b>Website:</b></label> <input type="text" name="editwebsite" value="' . $company->company_website . '">
<label><b>Logo:</b></label> <input type="file" name="editfile" id="file">
<input type="submit" value="Submit">
</form>
我得到的响应是“无效文件”。我累了上传各种图片,结果总是一样。
有人知道是什么原因造成的吗?
更新:
我按照建议将 enctype="multipart/form-data" 添加到表单中,显然无效文件错误现在消失了。 但是,我仍然没有在 wp-content/uploads 目录中看到上传。我看到它现在已经被最后一次修改了,所以脚本确实以某种方式改变了它,但我在任何地方都看不到实际上传的文件。我认为这可能与 wordpress 处理上传目录有关。
【问题讨论】:
-
在表单标签中添加属性 enctype="multipart/form-data"
标签: php file-upload