0

I used the basic PHP code to upload files like JPG, PNG and so on. To improve, I want to use WordPress secure and built-in functions and classes. I found the excellent wp_handle_upload () function which works great.

The only problem is that I can not move the uploaded file to the desired folder. Can anyone help me please? for example the location folder is wp-content\uploads\folder-name

1 Answer 1

-1

i do that with some line of code

add this to function.php to change the uploaded file dir

// change dir for anomaly images
function example_Dir( $param ){
    $param['path'] = '/home/yoursite/domains/yoursite.com/public_html/wp-content/uploads/example';
    $param['url'] = ' https://yoursite.com/wp-content/uploads/example';
    return $param;
}

here is the form :

<form action="" enctype="multipart/form-data" method="post" >
      <input type="file" name="file">
      <input type="submit">
</form>

then what PHP code you need is

if(isset($_FILES['file'])){
$file = $_FILES['file'];
    add_filter('upload_dir', 'example_Dir');
    $Result = wp_handle_upload($file  , $upload_overrides);
    remove_filter('upload_dir' , 'example_Dir');
}

done!

1
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Commented Sep 6, 2021 at 16:56

Your Answer

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

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