user10445138
user10445138

Reputation:

How to create a folder inside uploads folder and store uploaded files there?

I want uploaded files to be stored in wp-content/uploads/example/

How to edit this code so this can be done?

$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '/' . 
    basename($_FILES["fileToUpload"]["name"]);

Right now, it uploads files to wp-content/uploads/2018/10

I tried:

 $target_dir = wp_upload_dir();
 $target_file = $target_dir['path'] . '../../example' . 
    basename($_FILES["fileToUpload"]["name"]);

but it gives me file (uploadedfile.ext) uploaded in wp-content/uploads/2018 as exampleuploadedfile.ext

Upvotes: 0

Views: 1010

Answers (2)

user10445138
user10445138

Reputation:

Solution:

public function onixion_custom_dir_upload_function( $param ){ $mydir = '../../../example'; $param['path'] = $param['path'] . $mydir; $param['url'] = $param['url'] 
. $mydir; return $param; } 

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

It creates a folder example in the uploads folder, and stores uploaded files there.

If you're not in a class, you don't need

array( $this, 'onixion_custom_dir_upload_function' )

just write:

add_filter( 'upload_dir', 'onixion_custom_dir_upload_function');

If someone have same needs.

Upvotes: 0

Jaydeep Jagani
Jaydeep Jagani

Reputation: 74

Instead of this code

$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '../../example' .

Try this code. This code will change your upload directory path. I have used this to upload file in my custom folder in Upload Directory.

$target_dir = wp_upload_dir();
$target_file = $target_dir['basedir'].'/example'.basename($_FILES["fileToUpload"]["name"]);

Upvotes: 1

Related Questions