• Is there a plugin to manage file uploads. For instance I want to have a folder for PDFs, a folder for images, etc.

    Thank you,

    John

Viewing 3 replies - 1 through 3 (of 3 total)
  • In order to do this, we’ll have to edit the upload function in order to incorporate a variable for the type of file being uploaded. The function we’re interested in is wp-admin/admin-functions.php. The function is called wp_handle_upload()

    // Move the file to the uploads dir
    $new_file = $uploads['path'] . "/$filename";
    if ( false === @ move_uploaded_file($file['tmp_name'], $new_file) )
      die(printf(__('The uploaded file could not be moved to %s.'),  file['path']));

    and

    // Compute the URL
    $url = $uploads['url'] . "/$filename";

    The easiest thing we can do is add the extension into the upload path. This will mean that JPEGs and PDFs will be stored at something like https://mysite.com/blog/uploads/jpg/ or https://mysite.com/blog/uploads/pdf/, respectively.

    // Move the file to the uploads dir
    $new_file = $uploads['path'] . "/" .  $ext . "/$filename";
    if ( false === @ move_uploaded_file($file['tmp_name'], $new_file) )
      die(printf(__('The uploaded file could not be moved to %s.'),  file['path']));

    and

    // Compute the URL
    $url = $uploads['url'] . "/" . $ext . "/$filename";

    If your pathing is something else, you’ll have to put a switch function in there to determine the directory, or however it suits you. Good luck.

    [signature moderated Please read the Forum Rules]

    Thread Starter ronin898

    (@ronin898)

    Thank you

    Thread Starter ronin898

    (@ronin898)

    Is it possible to write a function that would pass this information to the wp_handle_upload() function so that I wouldn’t be altering the core. I know it’s better to do that, even if it’s not easier.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Managing Uploads’ is closed to new replies.