使用帖子ID创建上传文件夹

时间:2017-06-28 作者:Nick Davies

我正在为我们的员工创建一个在线区域,以便在WordPress中创建新帖子,与学生共享课程资源。我目前正在开发一个部分,学生可以通过该部分将文档上传到自定义帖子(前端表单),然后将这些上传内容存储在上传目录中。

目前,我已经将其上载到目录中,但我无法确定如何根据帖子ID创建新文件夹。

非常感谢您的任何想法。

include_once plugin_dir_path( __FILE__ ).\'upload.php\';

function assignment_uploads(){

  global $post;

  echo \'
    <form method="post" enctype="multipart/form-data" action="">
        <input type="file" name="file_upload">
        <input type="hidden" name="lesson_id" value="\'. $post->ID.\'">
        <input type="submit" name="upload_submitted">
    </form>
  \';
}
add_shortcode( \'student_submission_upload_form\', \'assignment_uploads\' );

// store post id in var
$lesson_id = $_POST[\'lesson_id\']; 

function create_uploads_folder() {
    $upload_dir = $uploadFolder[\'basedir\'];
    $upload_dir = $upload_dir . \'/submissions/\'.$lesson_id;
    if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0705 );
    }
}
register_activation_hook( __FILE__, \'create_uploads_folder\' );

// Upload file
if(!move_uploaded_file($_FILES[\'file_upload\'][\'tmp_name\'], $uploadFolder[\'basedir\'] . \'/submissions/\' . $lesson_id . \'/\' . $_FILES[\'file_upload\'][\'name\'])){
    die(\'Error uploading file - check destination is writeable\');
}

1 个回复
SO网友:Elizbar Yousuf

我为您编写了以下代码:

global $lesson_id;
$lesson_id = $_POST[\'lesson_id\']; 

function create_uploads_folder() {
    global $lesson_id;

    $upload_dir = wp_upload_dir();
    $upload_dir = $upload_dir[\'basedir\'];
    $upload_dir = $upload_dir . \'/submissions/\'. $lesson_id;
    if ( ! is_dir( $upload_dir ) ) {
        wp_mkdir_p( $upload_dir );
    }
}

register_activation_hook( __FILE__, \'create_uploads_folder\' );

// Upload file
$uploadFolder = wp_upload_dir();
if ( ! move_uploaded_file($_FILES[\'file_upload\'][\'tmp_name\'], $uploadFolder[\'basedir\'] . \'/submissions/\' . $lesson_id . \'/\' . $_FILES[\'file_upload\'][\'name\'])){
    die(\'Error uploading file - check destination is writeable\');
}

结束

相关推荐

将WordPress附件导入wp-Content/Uploads/中的自定义目录

我希望有人能阐明我试图解决的这个问题,即使只是简单地改变我的思维过程,告诉我如何解决这个问题。我正在将一个基于CakePHP的定制CMS web应用程序导入WordPress,方法是遍历数据库,进行大量SQL查询,构建数组,最后输出必要的XML文件,以便使用WordPress导入工具导入。目前我已经很好地导入了类别、标签和帖子/页面,没有问题,但问题似乎出在图像上。基于CakePHP的CMS中当前的图像格式是images/year/month/day/post_id/image_name.ext据我所知,