0

I have been trying various methods to upload a file to a wordpress site uploads directory (I gave it 777 permissions now just to be sure that I don't get permission problems). I have a custom php plugin where the code is placed.

I tried using wordpress functions but they don't seem to work.

So now I got a snippet of code in php that should handle the file upload (submitted by a form).

Here is my handling code:

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

And here is the form that is used to submit the 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>

The response I get is "Invalid File". I tired uploading all kinds of images and the result is always the same.

Does anyone know what could be causing this?

UPDATE:

I followed the advice to add enctype="multipart/form-data" to the form and obviously the invalid file error is gone now. However, I still don't see the upload in the wp-content/uploads directory. I see that it has been last modified now, so the script did alter it somehow, but I don't see the actual uploaded file anywhere. I think it might have something to do with wordpress processing the uploads directory.

1
  • 3
    add attribute enctype="multipart/form-data" in form tag Commented Feb 10, 2014 at 12:56

1 Answer 1

1

You must add the attribute enctype="multipart/form-data" in form tag to send files with the form.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This seems to have solved the issue! Now I don't get an error. However the file is still not seen in the uploads directory. It created the folders in the upload dir and I can see that they have been modified now, but I don't see the actual file there. Very strange.
It works, I changed the upload directory. Thanks for your answer.
Could be a problem with write privileges on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.