将文件从一个目录移动到另一个目录

时间:2016-04-10 作者:user1443216

我有这个功能:

function attachment_selectbox_edit($form_fields, $post) {

$select_options = array(
    "no" => "No",
    "yes" => "Yes"
);

// get the current value of our custom field
$current_value = get_post_meta($post->ID, "_my_select", true);

// build the html for our select box
$mySelectBoxHtml = "<select name=\'attachments[{$post->ID}][my_select]\' id=\'attachments[{$post->ID}][my_select]\'>";
foreach($select_options as $value => $text){

    // if this value is the current_value we\'ll mark it selected
    $selected = ($current_value == $value) ? \' selected \' : \'\';

    // escape value for single quotes so they won\'t break the html
    $value = addcslashes( $value, "\'");

    $mySelectBoxHtml .= "<option value=\'{$value}\' {$selected}>{$text}</option>";
}
$mySelectBoxHtml .= "</select>";

// add our custom select box to the form_fields
$form_fields["my_select"]["label"] = __("Move the file?");
$form_fields["my_select"]["input"] = "html";
$form_fields["my_select"]["html"] = $mySelectBoxHtml;

return $form_fields;
}
add_filter("attachment_fields_to_edit", "attachment_selectbox_edit", null, 2);
当$current\\u value==“yes”时,我想将文件从“uploads”目录移动到“uploads/new目录”。

你能帮帮我吗?

1 个回复
SO网友:majick

似乎还无法找到正确的筛选器来处理此问题(编辑表单正在保存的位置),尤其是因为attachment_fields_to_edit 可在多个地方使用。但也许你已经知道了?无论如何,这会给你一个好的开始。。。

add_filter(\'??attachment_fields_save_filter??\',\'move_attachment_directory\',10,2);

function move_attachment_directory($form_fields,$post) {

$attach_id = $post->ID;
if (!isset($_POST[\'attachments\'][$attach_id][\'my_select\'])) {return;}
if ($_POST[\'attachments\'][$attach_id][\'my_select\'] == \'no\') {return;}

// set directories
$uploaddir = wp_upload_dir();
$newdirectory = \'new-directory\'; // or whatever it is

// get attachment metadata
$attachdata = wp_get_attachment_metadata($attach_id);

// move original image
$filepath = trailingslashit($uploaddir).$attachdata[\'file\'];
$newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$attachdata[\'file\'];
rename($filepath,$newfilepath);
$attachdata[\'file\'] = trailingslashit($newdirectory).$attachdata[\'file\'];

// move each attachment size too
foreach ($attachdata[\'sizes\'] as $size => $data) {
    $file = $data[\'file\'];
    $filepath = trailingslashit($uploaddir).$data[\'file\'];
    $newfilepath = trailingslashit($uploaddir).trailingslashit($newdirectory).$data[\'file\'];
    rename($filepath,$newfilepath);
    $data[\'file\'] = trailingslashit($newdirectory).$data[\'file\'];
    $attachdata[\'sizes\'][$size] = $data;
}

// update attachment metadata
wp_update_attachment_metadata($attach_id,$attachdata);

}
注意:如果您将媒体按月/年进行排序,我认为这不会像现在这样起作用。

在任何情况下,这都是未经测试的代码,可能有路径错误等,因此可能需要在测试环境中进行一些工作,但它表明这是可以完成的,希望它能让您进入下一步。。。

相关推荐

Apply filters on date format

我使用Wordpress主题,其中格式日期和时间直接编码为两种格式:<?php the_time(\'d M\'); ?> <?php the_time(\'H:i\'); ?> 我想要的:显示wordpress选项中定义的日期和时间我不想在子主题中创建修改过的模板,我更喜欢使用函数。我已经在函数中添加了php。php此代码有效:add_filter( \'the_time\', \'changer_date_format\', 10, 1 ); //o