以自定义张贴形式上传图像

时间:2011-10-22 作者:Michal

我有在我的主题文件中发布的自定义表单。我还需要上传照片(视频也一样,但现在照片更重要了),我用wp handle上传功能准备了表格。它正在工作,图像正在上传,但它们没有插入到帖子中。请帮帮我。这里有一个完整的代码http://cdpst.net/s8i5jsfuf 以下是相关位:

if ( $_POST[\'question_post\'] == \'1\') {
    $question_title = $_POST[\'question_title\'];
    $question_content = $_POST[\'question_content\'];
    $question_category = $_POST[\'cat\'];
    $question_tags = $_POST[\'question_tags\'];

    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            $newupload = insert_attachment($file,$pid);
            // $newupload returns the attachment id of the file that
            // was just uploaded. Do whatever you want with that now.
        }
    }

    // ...
}

2 个回复
SO网友:Delphi Electronice

假设您的文件变量名为“files”,可以包含多个输入(即name=“files[]”)

1) 首先上传文件

$uploads = wp_upload_dir();
  $upload_file_url = array();
  foreach ($_FILES[\'files\'][\'name\'] as $key => $value_data)
    {
        $errors_founds = \'\';

        if ($_FILES[\'files\'][\'error\'][$key] != UPLOAD_ERR_OK)
            $errors_founds .= \'Error uploading the file!<br />\';

        if (!array_key_exists($_FILES[\'files\'][\'type\'][$key], $mime))
            $errors_founds .= \'Invalid file type!<br />\';

        if ($_FILES[\'files\'][\'size\'][$key] == 0)
            $errors_founds .= \'Image file it\\\'s empty!<br />\';

        if ($_FILES[\'files\'][\'size\'][$key] > 524288)
            $errors_founds .= \'Image file to large, maximus size is 500Kb!<br />\';

        if(!is_uploaded_file($_FILES[\'files\'][\'tmp_name\'][$key]))
             $errors_founds .= \'Error uploading the file on the server!<br />\';

        if ($errors_founds == \'\')
            {
                //Sanitize the filename (See note below)
                $remove_these = array(\' \',\'`\',\'"\',\'\\\'\',\'\\\\\',\'/\');
                $newname = str_replace($remove_these,\'\', $_FILES[\'files\'][\'name\'][$key]);
                //Make the filename unique
                $newname = time().\'-\'.$newname;
                //Save the uploaded the file to another location

                $upload_path = $uploads[\'path\'] . "/$newname";
                move_uploaded_file($_FILES[\'files\'][\'tmp_name\'][$key], $upload_path);
                $upload_files_url[$upload_path] = $uploads[\'url\'] ."/$newname";
            }
    }
2)处理成功上传的文件

if (count($upload_files_url)  > 0)
    {
        foreach ($upload_files_url as $filename_path => $upload_file_url)
            {
                $wp_filetype = wp_check_filetype(basename($filename_path), null );
            $attachment = array(
                                \'post_mime_type\' => $wp_filetype[\'type\'],
                                \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($filename_path)),
                                \'post_content\' => \'\',
                                \'post_status\' => \'inherit\'
                                );
            $attach_id = wp_insert_attachment( $attachment, $filename_path);
            // you must first include the image.php file
            // for the function wp_generate_attachment_metadata() to work
            require_once(ABSPATH . \'wp-admin/includes/image.php\');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename_path );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            }
    }

SO网友:soulseekah

首先,应该注意的是,您的表单似乎没有MAX_FILE_SIZE 价值请参阅POST method uploads PHP手册的第节。

其次,没有这样的函数insert_attachment(), 功能是wp_insert_attachment(). 它要求:

$attachment 包含文件属性等的数组$filename 上传文件的(必须在WordPress中uploads 目录(wp_upload_dir()), 所以你必须把它转移过来move_uploaded_file() 例如)和$parent_post_id, 这大概是您当前的帖子ID,您可以使用get_the_ID() 模板函数的Codex page for wp_insert_attachment() 有一个关于如何将文件(特别是图像)附加到帖子的很好的示例。

结束

相关推荐

Hide password protected posts

如果我创建一篇帖子并将可见性设置为“密码保护”,它仍然会显示在首页和提要中。有没有可能从总体视图中完全隐藏帖子,但仍然允许快速访问某些人,而无需为他们创建帐户?(这在WP 3.0.4中)