Support

Account

Home Forums Feature Requests Uploaded Files Location

Solving

Uploaded Files Location

  • I would like to upload file attachments to my custom-post type, but NOT store them in the WordPress gallery.

    I’d like to specify a location to put them within the field settings.

    For example, if I specify a folder called “artwork”, then the files would be uploaded to “uploads/acf/artwork”. This would also NOT index it into the Gallery.

    The gallery is way too cluttered as it is.

  • I’m sure this would be useful but I think it’s going outside the scope of what ACF is intended for. I will flag this topic for the developer’s attention.

    For changing the upload path see this documentation. https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir

  • One way might be using filters. Perhaps he can create hooks that I can use to set where the file goes and if it gets added to the gallery.

    • Elliot

    • July 22, 2015 at 2:35 pm

    Hi @dnavarrojr

    Thanks for the thread.

    I wonder if it is possible to use the filet which @hube2 has linked to, and within the filter, look at the $_FILES and $_POST data to determine if the file was uploaded via ACF.

    This way you can conditionally change the upload dir.

    Let me know if you are able to play around with this, and what you find.

  • This is how i was able to change the uploads directory for a specific ACF field. I have left some extra code for debugging.

    1 . acf/upload_prefilter/name=my_acf_upload_fieldname’ change my_acf_upload_fieldname to the name of your ACF field.
    2. $mydir = ‘/newdirectory’; // this currently writes to /uploads/newdirectory change newdirectory to your directory name.
    3. my_acf_upload_prefilter is called before the uploading of a file (whatever you name your ACF file field). Then it calls add_filter(‘upload_dir‘, ‘my_upload_directory’); which changes the directory for the upload of that file. but otherwise files upload to whatever is your default wp directory.

    function my_acf_upload_prefilter( $errors, $file, $field ) {
        
        // only allow admin
        if( !current_user_can('manage_options') ) {
            
            // this returns value to the wp uploader UI
            // if you remove the ! you can see the returned values
            $errors[] = 'test prefilter';
            $errors[] = print_r($_FILES,true);
            $errors[] = $_FILES['async-upload']['name'] ;
            
        }
        //this filter changes directory just for item being uploaded
        add_filter('upload_dir', 'my_upload_directory');
        
        // return
        return $errors;
        
    }
    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter');
    
    function my_upload_directory( $param ){
        $mydir = '/newdirectory';
    
        $param['path'] = $param['basedir'] . $mydir;
        $param['url'] = $param['baseurl'] . $mydir;
    
    	// if you need a different location you can try one of these values
    /*	
        error_log("path={$param['path']}");  
        error_log("url={$param['url']}");
        error_log("subdir={$param['subdir']}");
        error_log("basedir={$param['basedir']}");
        error_log("baseurl={$param['baseurl']}");
        error_log("error={$param['error']}"); 
    */
    
        return $param;
    }
    • Elliot

    • July 29, 2015 at 10:52 am

    Nice one @liquidbook

  • That works. However, here’s my biggest issue…

    I am using ACF like crazy to create tons of extensions to our web site. And having the code separate from the “form” makes it difficult to maintain.

    The ability to attach the code for a given field to that field in the ACF builder would now only allow me to more easily maintain the code whenever I make changes, but it would allow me to export/import the associated code with each form.

    Maybe this is better off as an addon, but I would really love to see the ability to add JS and PHP code to each field in the ACF form builder.

  • Perfect, I’ve spent hours looking into download managers etc.. I’ve used this little script with a .htaccess and download file modification to protect only my ACF uploads for logged in users only.
    PM if anyone interested..

    .htaccess control, and download file.

    • Rics

    • February 7, 2016 at 2:09 am
    This reply has been marked as private.
    • dtx211

    • January 24, 2017 at 12:09 am

    Is this method still working in the latest ACF version?

    I was working on a site locally until my database crashed last week. I used a customised version of your code for the last few weeks, but since I restored the site from a backup a few days ago I keep getting a 500 error when this function runs.

    Here’s my code:

    function my_upload_directory( $param ) {
      $mydir          = '/music';
      $param['path']  = $param['basedir'] . $mydir;
      $param['url']   = $param['baseurl'] . $mydir;
      return $param;
    }
    
    function my_acf_upload_prefilter( $errors, $file, $field ) {
      // change the upload directory
      add_filter('upload_dir', 'my_upload_directory');
      return $errors;
    }
    add_filter('acf/upload_prefilter/name=music-cover-art', 'my_acf_upload_prefilter');
    

    Any idea where the problem might lie? I’m using the exact same code that worked before, the only thing that has changed seems to be the ACF version (5.5.3 to 5.5.5)?

  • Just wanna add that I had trouble getting this to work aswell. But I’m using Sage theme from Roots, and you need to hook into the filters like this:

    add_filter('upload_dir', __NAMESPACE__ . '\\my_upload_directory');
    add_filter('acf/upload_prefilter/name=preview_video', __NAMESPACE__ . '\\my_acf_upload_prefilter');

    Otherwise it won’t be able to locate the filters.

  • I had the same issue as @dtx211. I solved the problem by passing an array with the filters:

    add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', array( $this, 'field_name_upload_prefilter' ) );

    AND

    add_filter('upload_dir', array( $this, 'field_name_upload_dir' ) );

    Also ensure, that the upload directory has the right access rights and maybe already exists.

  • Sorry to dig up an old post, but I can’t seem to get this to work with sub fields (through a repeater).

    I know John suggested the field key on another thread but I’ve had no luck.

    Thanks

    • lupet

    • October 28, 2018 at 10:03 pm

    You can help me with your script in latest WordPress version?

    Regards

    • viral

    • November 22, 2018 at 11:21 pm

    Sorry for digging old post but i’m facing same issue. I keep getting a 500 error on “async.php” when this function runs. Without this filter i was able to upload files.

    I tried all possible solutions including
    – Memory increasing in .htaccess, php etc
    – passing array as parameter as suggested above
    – folder permission changed

    Is there anyone can help me on this ?

Viewing 16 posts - 1 through 16 (of 16 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.