function save_files($post_id = 0)
 {
     // bail early if no $_FILES data
     if (empty($_FILES['acf']['name'])) {
         return;
     }
     // upload files
     acf_upload_files();
 }
Exemple #2
0
function acf_upload_files($ancestors = array())
{
    // vars
    $file = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => '', 'size' => '');
    // populate with $_FILES data
    foreach (array_keys($file) as $k) {
        $file[$k] = $_FILES['acf'][$k];
    }
    // walk through ancestors
    if (!empty($ancestors)) {
        foreach ($ancestors as $a) {
            foreach (array_keys($file) as $k) {
                $file[$k] = $file[$k][$a];
            }
        }
    }
    // is array?
    if (is_array($file['name'])) {
        foreach (array_keys($file['name']) as $k) {
            $_ancestors = array_merge($ancestors, array($k));
            acf_upload_files($_ancestors);
        }
        return;
    }
    // bail ealry if file has error (no file uploaded)
    if ($file['error']) {
        return;
    }
    // assign global _acfuploader for media validation
    $_POST['_acfuploader'] = end($ancestors);
    // file found!
    $attachment_id = acf_upload_file($file);
    // update $_POST
    array_unshift($ancestors, 'acf');
    acf_update_nested_array($_POST, $ancestors, $attachment_id);
}