2012-12-13 58 views
0

我工作的插件,在後端添加網站或帖子和簡短說明的網址,並在前端顯示然後在一個小部件,我有小圖像的按鈕發帖上傳,但它didi't制定出在正常的PHP,但相同的代碼,做工精細...圖片上傳在WordPress管理選項頁面不工作

$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> 

回答

0

問題出在文件上傳的URL,這是我應該如何解決..,現在它工作正常...

$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]; 
            } 
1

形式你應該看看wp_handle_upload爲這個。
這裏給出的例子非常有用。

保存的網址,您可以使用下面幾行:

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

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

希望它能幫助!

+0

我試過這種方式,給了我這樣的「文件一個錯誤是空的。請上傳更實質的內容。這個錯誤也可能是由於您的php.ini中的上傳被禁用或者post_max_size被定義爲小於php.ini中的upload_max_filesize所致。 (長度= 212) – wordpresrox

+1

您可以發佈處理完成的代碼嗎? –

+0

我想出了錯誤 – wordpresrox