image upload error wordpress

时间:2015-05-21 作者:Dhiraj Patel

我正在尝试上载图像,它已成功上载,但当我尝试在phpmysql中访问它时,链接如下:http://localhost/job/?attachment_id=40

我想按如下方式存储此图像url:http://localhost/job/wp-content/uploads/2015/05/Koala.jpg

以下是上载图像的代码:

if(!empty($_FILES[\'photo_rcb\'])) {
                                $file   = $_FILES[\'photo_rcb\'];
                                $upload = wp_handle_upload($file, array(\'test_form\' => false));
                              //  print_r($upload);exit;
                                if(!isset($upload[\'error\']) && isset($upload[\'file\'])) {
                                    $filetype   = wp_check_filetype(basename($upload[\'file\']), null);
                                    $title      = $file[\'name\'];
                                    $ext        = strrchr($title, \'.\');
                                    $title      = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
                                    $attachment = array(
                                        \'post_mime_type\'    => $wp_filetype[\'type\'],
                                        \'post_title\'        => addslashes($title),
                                        \'post_content\'      => \'\',
                                        \'post_status\'       => \'inherit\',
                                        \'post_author\'       => $user_id
                                    );

                                        $attach_key = \'document_file_id\';
                                        $attach_id  = wp_insert_attachment($attachment, $upload[\'file\']);


                                        update_user_meta($user_id,\'photo_rcb\', $attach_id);
                                  }

1 个回复
SO网友:paxamus

使用wp\\u handle\\u upload时,图像应以该格式上载。现在还不清楚你是如何http://localhost/job/?attachment_id=40

我建议吃点类似Handling the File Upload 本职位部分:

结束