2014-02-10 145 views
0

我需要将徽标上传功能添加到WordPress上的自定义php插件。 我发现WordPress的抄本此代码:无法使用wordpress上传功能上传文件

if (! function_exists('wp_handle_upload')) require_once(ABSPATH . 'wp-admin/includes/file.php'); 
$uploadedfile = $_FILES['editfile']; 
$upload_overrides = array('test_form' => false); 
$movefile = wp_handle_upload($uploadedfile, $upload_overrides); 
if ($movefile) { 
    echo "File is valid, and was successfully uploaded.\n"; 
    var_dump($movefile); 
} else { 
    echo "Possible file upload attack!\n"; 
} 

下面的形式:

<form action="' . site_url('/edit-company/?cid=' . $_GET['cid']) . '" 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> 

如果我将文件提交到这一点,使我有以下错误:

File is valid, and was successfully uploaded. array(1) { ["error"]=> string(212) "File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini." }

的我提交的文件大小约为50kb。上传更大或更小的文件似乎没有什么区别。

我没有问题上传WordPress的任何大小的文件管理员我不会认为php.ini或张贴大小是造成这种情况。

如果您对此有任何提示或信息,请让我知道。我现在已经停留了很长一段时间了。尝试在香草PHP中使用简单的上传脚本,但这似乎并没有工作,所以我回来试图用wp函数完成它。

更新: - 试着上传文件夹的权限设置为777 - 没有帮助

+1

我认为你需要,你要上传的文件对文件夹的权限。 –

+0

将wp uploads文件夹的权限设置为775是否安全? –

+1

是的,它可以安全地设置权限775. –

回答

3

请确认您的上传表单是不是另一种形式的内部。因为一旦我得到和你一样的错误,并且问题是因为我的上传表单位于另一个表单中。

希望这可以帮助。

[更新]

您的形式是好的,但我看到你赶上输入字段名和后变量名是不同

$uploadedfile = $_FILES['file']; 

<input type="file" name="editfile" id="file"> 

这些名称不能是不同的。尝试编辑这样

$uploadedfile = $_FILES['editfile']; 

[更新]

还请添加到您的表单的属性

enctype="multipart/form-data" 
+0

我用表单代码更新了原始问题。对我来说,似乎一切都好。 –

+0

谢谢,但那只是我复制未经编辑的示例代码,而不是我实际使用的具有相同名称'editfile'的修改后的代码。对不起,没有注意到这一点。 –

+1

下次请复制您的真实代码以避免这种情况。 :) 顺便说一句,你的另一个输入字段呢? 他们工作正常.. ?? – yogipriyo