0

I'm not very expert of wordpress and I'm trying to create a frontend form sending information to my php function through ajax. I'd need help about uploading images and files as attachments of the post.

My jQuery / Ajax is sending everything correctly to my php file. The only thing I don't understand is why images are uploaded and files aren't being in the same function?

My html inputs are relatively named:

  • name="moreimages"
  • name="morefiles"

I use this php code for both images and files

if ($_FILES)
    {
        // Get the upload images
        $images = $_FILES['moreimages'];
        foreach ($images['name'] as $key => $value)
        {
            if ($images['name'][$key])
            {
                $image = array(
                    'name' => $images['name'][$key],
                    'type' => $images['type'][$key],
                    'tmp_name' => $images['tmp_name'][$key],
                    'error' => $images['error'][$key],
                    'size' => $images['size'][$key]
                );
                $_FILES = array("moreimages" => $image);
                foreach ($_FILES as $image => $array)
                {
                    $newupload = project_images($image,$pid);
                }
            }
        }
        // Get the upload attachment files
        $files = $_FILES['morefiles'];
        foreach ($files['name'] as $key => $value)
        {
            if ($files['name'][$key])
            {
                $file = array(
                    'name' => $files['name'][$key],
                    'type' => $files['type'][$key],
                    'tmp_name' => $files['tmp_name'][$key],
                    'error' => $files['error'][$key],
                    'size' => $files['size'][$key]
                );
                $_FILES = array("morefiles" => $file);
                foreach ($_FILES as $file => $array)
                {
                    $uploadfile = project_file($file,$pid);
                }
            }
        }
    }

function project_images($file_handler, $pid)
    {
        if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        $image_id = media_handle_upload( $file_handler, $pid );
        return $image_id;
    }
function project_file($file_handler, $pid)
    {
        if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        $file_id = media_handle_upload( $file_handler, $pid );

        //here is the only difference where I update the post meta

        update_post_meta($file_id,'is_prj_file','1');
        return $file_id;
    }

I can't see the problem. Can you drive me maybe suggesting a similar question answered? Thanks.

  • What are the extensions of the files you're trying to upload? – Tim Malone May 4 '16 at 22:20
  • For images: image/png, image/jpeg, image/gif | For files: .doc,.docx,.xls,.xlsx,.ppt,.pptx. @TimMalone – Kleeia May 4 '16 at 22:22
  • Hmm those files should be ok, I think. Do they work if you upload them through the normal WP media library? – Tim Malone May 4 '16 at 22:26
  • How can I discover which file are allowed and which not? Even if I upload simple .doc it doesn't seem working... @TwerkingN00b – Kleeia May 4 '16 at 22:28
  • Yes they work If I use the WP Dashboard. Eventually Is there another method I can try to achieve it? @TimMalone – Kleeia May 4 '16 at 22:29
2

I mentioned in a comment how it's important to debug your code. Here's why:

The images are added first. In the image adding section, you're running this line of code:

$_FILES = array("moreimages" => $image);

Then when you get to your routine that adds the files, you start with this:

$files = $_FILES['morefiles'];

Can you see what's wrong here? At this point, $_FILES only contains "moreimages" and nothing else, because you overwrote it earlier.

You could simply create a new variable rather than resetting $_FILES (eg. $my_processed_images = array("moreimages" => $image); and then foreach ($my_processed_images...), but there's a lot of other things that can be done to make this code more redundant and easier to follow too.

A quick point on debugging: print_r() is your friend. For example, if you're expecting a variable to be holding something, print_r($_FILES) so you can see if it really is. This will help avoid hours of head scratching :)

  • That's very helpful man! I come back to study now!!! Thanks again! – Kleeia May 4 '16 at 22:50
  • I'm very sorry to bother you again. I'm just processing all the informations you've provided me. What I've understood is that if I change $_FILES into some other variable I could differentiate arrays and avoid to overwrite them each other. But I think I'm missing something because even so I can't achieve it. It uploads only $_FILES ignoring something like $my_processed_images = array("moreimages" => $image);. Going crazy... – Kleeia May 5 '16 at 11:10
  • Did you change the foreach on the next line to refer to $my_processed_images as well? What are you going thru is some basic PHP, I would recommend looking online for a good PHP tutorial to brush up on some of this as you will find it much easier to code then :) If you keep getting stuck, consider posting some specific questions with the problems you are having on StackOverflow – Tim Malone May 5 '16 at 20:53
1

If you are not very familiar and want a plugin that can do all that for you using drag and drop, try WP-TYPES and their CRED plugin.

Just don't mind their support, it's really frustrating.

  • Thank you but I'd rather learn how to code it! I want understand and improve my knowledge. Thanks again. – Kleeia May 5 '16 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.