0

我正在开发插件,该插件在后端添加网站或帖子的网址和简短描述,然后在前端显示然后在小部件中,我有用于上传帖子小图像的按钮,但它没有成功,但相同的代码在普通 php 中工作正常...

 $upload_errors = array(
                              // http://www.php.net/manual/en/features.file-upload.errors.php
                                UPLOAD_ERR_OK               => "No errors.",
                                UPLOAD_ERR_INI_SIZE     => "Larger than upload_max_filesize.",
                                UPLOAD_ERR_FORM_SIZE    => "Larger than form MAX_FILE_SIZE.",
                                UPLOAD_ERR_PARTIAL      => "Partial upload.",
                                UPLOAD_ERR_NO_FILE      => "No file.",
                                UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
                                UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
                                UPLOAD_ERR_EXTENSION    => "File upload stopped by extension."
                             );


                           // process the form data
                           $tmp_file = $_FILES['file_upload']['tmp_name'];
                           $target_file = basename($_FILES['file_upload']['name']);
                           $upload_dir = "uploads";

                           // You will probably want to first use file_exists() to make sure
                           // there isn't already a file by the same name.

                           // move_uploaded_file will return false if $tmp_file is not a valid upload file 
                           // or if it cannot be moved for any other reason
                           if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
                           $message = "File uploaded successfully.";
                            } else {
                                $error = $_FILES['file_upload']['error'];
                                $message = $upload_errors[$error];

                                }

这是用于上传图片的表单

  <form action='' method='post' name="text_form" onsubmit="return Blank_TextField_Validator()" enctype="multipart/form-data">
                             <table class='form-table'><tr valign='top'>  
                             <th scope='row'><lable for='new_Directory_name'>Enter the Title:</lable></th>
                             <td><input type='text' id='newtextchange' name='newtextchange' size="100" /></br></td>
                             </tr>
                             <tr>
                             <th scope='row'><lable for='new_Directory_name'>Enter the Description:</lable></th>
                             <td><textarea rows="4" cols="50" name='textarea1'>

                             </textarea></br></td>
                             </tr>
                             <tr>
                             <th scope='row'><lable for='new_Directory_name'>Enter the URL:</lable></th>
                             <td><input type='text' id='newtextchange1' name='newtextchange1' size="100" /></br></td>
                             </tr>
                             <tr>
                             <th scope='row'><lable for='new_Directory_name'>Upload image:</lable></th>

                             <td> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><input type="file" name="file_upload" /><br><br><input id='addtobow' class='button-secondary action' type='submit' value='Add to Best of web' name='submit'/></td>
                             </tr>
                             </table>


                          </form>
4

2 回答 2

1

你应该看看wp_handle_upload这个。
那里给出的例子非常有用。

保存url,您可以使用以下行:

$upload_overrides = array( 'test_form' => false );
$source = wp_handle_upload( $_FILES['file'], $upload_overrides );

if ( $source )
    $input = serialize( $source );

希望能帮助到你!

于 2012-12-13T09:55:14.430 回答
0

问题在于文件上传网址,这就是我修复它的方法..,现在它工作正常...

$tmp_file = $_FILES['file_upload']['tmp_name'];
                          $target_file = basename($_FILES['file_upload']['name']);
                          //$upload_dir = "D:\softwares_installed\wamp\www\wordpress\wp-content\plugins\bestofweb\uploads";
                            $upload_dir =ABSPATH . "wp-content/plugins/bestofweb/uploads";
                            $up_urlp1="/wp-content/plugins/bestofweb/uploads";
                          // You will probably want to first use file_exists() to make sure
                          // there isn't already a file by the same name.

                          // move_uploaded_file will return false if $tmp_file is not a valid upload file 
                          // or if it cannot be moved for any other reason
                          if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {
                                //$message = "File uploaded successfully.";
                                //echo $upload_dir."/".$target_file;
                                //echo  bloginfo('wpurl');
                                $up_url= $up_urlp1."/".$target_file;
                                //echo $up_url;
                                //if($message == "File uploaded successfully.")
                                //  {
                                //  $imgpath=$upload_dir.

                                //  }

                                   } else {
                                     $error = $_FILES['file_upload']['error'];
                                    // $message = $upload_errors[$error];
                                   }
于 2012-12-18T07:44:30.670 回答