Saint Dave
Saint Dave

Reputation: 567

Can't upload a file using wordpress upload functions

I need to add logo upload functionality to a custom php plugin on wordpress. I found this code on wordpress codex:

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";
}

Here is the form:

<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>

If I submit a file to this it gives me the following error:

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." }

The file I am submitting is about 50kb in size. Uploading bigger or smaller files doesn't seem to make a difference.

I have no problem uploading files of any size in Wordpress admin do I wouldn't think php.ini or post max size is causing this.

If you have any tips or info on this please let me know. I have been stuck on this for quite some time now. Tried using simple upload script in vanilla PHP but that also doesn't seem to work so I am back at trying to get it done with wp functions.

UPDATE: - tried setting the permissions of the upload folder to 777 - no help

Upvotes: 0

Views: 2573

Answers (1)

yogipriyo
yogipriyo

Reputation: 635

Please make sure that your upload form is not inside another form. Because once I got the same error as yours and the problem is because my upload form located inside another form.

Hope this can help.

[updated]

Your form is fine, but I see that the input field name and the post variable name that you catch is different

$uploadedfile = $_FILES['file'];

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

Those names must not be different. Try to edit it like this

$uploadedfile = $_FILES['editfile'];

[updated]

And also please add this to your form's attribute

enctype="multipart/form-data"

Upvotes: 3

Related Questions