Home CMS Tutorial WordPress Where are the files uploaded by wordpress?

Where are the files uploaded by wordpress?

Jul 17, 2019 am 10:17 AM
wordpress

Where are the files uploaded by wordpress?

wordpress上传的文件在哪?

WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。

WordPress上传默认设置

有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。

为Custom Post Type设置上传目录

假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可

function custom_upload_directory( $uploads ) {
    $id = $_REQUEST['post_id'];
        $parent = get_post( $id )->post_parent;
 
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $subdir = 'slides';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
    }
    return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
Copy after login

将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。

将文件保存到插件目录

下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。

/**
 * Change Upload Directory for Custom Post-Type
 *
 * This will change the upload directory for a custom post-type. Attachments will
 * now be uploaded to an "uploads" directory within the folder of your plugin. Make
 * sure you swap out "post-type" in the if-statement with the appropriate value...
 */
function custom_upload_directory( $args ) {
  
    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;
  
    // Check the post-type of the current post
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $args['path'] = plugin_dir_path(__FILE__) . "uploads";
        $args['url']  = plugin_dir_url(__FILE__) . "uploads";
        $args['basedir'] = plugin_dir_path(__FILE__) . "uploads";
        $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads";
    }
    return $args;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
Copy after login

如果要以年月形式保存,修改一下代码即可

$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir'];
$args['url']  = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];
Copy after login

这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/

为后台管理页面设定upload_dir

用wp_editor在后台管理页面(比如用add_menu_page创建的页面)创建一个媒体上传功能,希望所有从该页面上传的文件都保存到wp-content/uploads/myfolder目录下。

由 于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非 post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer() 函数获取引荐url,如果正好与我们的option page url想等,就更该目录。

function custom_upload_directory( $uploads ) {
 
   if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){
        $subdir = 'myfolder';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
   }
   return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
Copy after login

参考信息

filter:upload_dir是在wp_upload_dir()函数中调用的

$upload_dir = wp_upload_dir();
$upload_dir now contains something like the following (if successful)
Array (
    [path] => C:\path\to\wordpress\wp-content\uploads\2010\05
    [url] => http://example.com/wp-content/uploads/2010/05
    [subdir] => /2010/05
    [basedir] => C:\path\to\wordpress\wp-content\uploads
    [baseurl] => http://example.com/wp-content/uploads
    [error] =>
)
Copy after login

更多WordPress技术文章,请访问WordPress教程栏目!

The above is the detailed content of Where are the files uploaded by wordpress?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to add comments to WordPress How to add comments to WordPress Apr 20, 2025 am 11:42 AM

The steps to add a comment in WordPress are as follows: Enable Comments: Check "Allow Comments" to enable comments for a specific article or all articles. Set comment options: Customize comment approval, paging, nested comments and other settings. Add Comment Form: Use the provided code to add the Comment Form to the desired location. Show Comments: Use the provided code to display submitted comments. Reply to comments: Reply to a single comment through the Reply link, or reply to multiple comments using a batch operation. Manage comments: View, delete, approve, and tag comments in the Comments tab.

How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

Recommended product registration plug-in for easy-to-use wordpress Recommended product registration plug-in for easy-to-use wordpress Apr 20, 2025 am 08:15 AM

There is no perfect WordPress product registration plugin, the choice should be based on actual needs and website size. Recommended plug-ins include: MemberPress: powerful but high-priced, complex configuration Restrict Content Pro: Focus on content restrictions and member management, cost-effective Easy Digital Downloads: Sell digital products, and users register as additional functions

WordPress website is online but cannot be searched WordPress website is online but cannot be searched Apr 20, 2025 am 09:00 AM

Reasons why WordPress websites cannot be found in search engines: 1. Indexing issues; 2. Content issues; 3. Website technical issues; 4. Link issues; 5. Other issues such as geographical restrictions, website name and social media presence.

WordPress website account login WordPress website account login Apr 20, 2025 am 09:06 AM

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

How to locate pictures in wordpress How to locate pictures in wordpress Apr 20, 2025 am 10:03 AM

WordPress provides the following ways to locate images: Image identification number: Hover the image in the media library to display the identification number (ID). Image Title: Enter a title in the Search Media field to locate the image. Image URL: In the image editor, the image URL is displayed in the upper right corner. Location Identifier: In the Advanced tab of block settings, find the location identifier in the CSS Class field of the specified image location.

How to close comments with wordpress How to close comments with wordpress Apr 20, 2025 am 11:54 AM

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

See all articles