2012-02-08 33 views
2

我有一個WordPress插件在我的工作,我似乎已經打了一個問題。 我需要用戶能夠上傳圖像,而不是我一直得到這個問題:PHP圖片上傳(WordPress的自定義插件)

上傳目錄不可寫或不存在。

警告: move_uploaded_file(http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png) [function.move上傳文件]:未能打開流:HTTP包裝 不在線支持 /home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php 可寫連接39


警告:move_uploaded_file()以[function.move上傳文件]:無法 移動 '/ TMP/phpowqMlu' 至 「http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes /productimages/78.105.162.431328735863.png' in /home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php在線39上的 上傳文件時出現錯誤,請重試! 您已成功將test_title到你的產品列表


目錄肯定是存在的,我甚至改變了777權限的測試。

的products.php文件看起來是這樣的:

<? 
if ('POST' == $_SERVER['REQUEST_METHOD']) 
{ 
global $wpdb; 

$product_title = $_POST['title']; 
$product_url = $_POST['url']; 
$product_btn_text = $_POST['btn_text']; 

// CREATE UNIQUE NAME FOR IMAGE 
$remote_addr = $_SERVER['REMOTE_ADDR']; 
$time = time(); 
$new_name = $remote_addr; 
$new_name .= $time; 


// IMAGE UPLOAD 

$upload_dir = "http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/"; 

if (file_exists($upload_dir) && is_writable($upload_dir)) { 
     echo "<br /> Directory exists and is fine.... <br />"; 
} 
else { 
     echo "Upload directory is not writable, or does not exist. <br />"; 
} 

$uploadedfile = $_FILES['image_file']['name']; 
$extension = explode(".", $uploadedfile); 
$extensiontype = $extension['1']; 

$target_path = $upload_dir; 
$target_path = $target_path .$new_name .'.'.$extensiontype; 

if(move_uploaded_file($_FILES['image_file']['tmp_name'], $target_path)) { 
     echo "The file ". basename($_FILES['image_file']['name']). 
     " has been uploaded <br />"; 
} else{ 
    echo "There was an error uploading the file, please try again! <br />"; 

} 

$product_img = $new_name.'.'.$extensiontype; 

//ADD TO DATABASE 
    $wpdb->query("INSERT INTO wp_rec_amazon_product (product_title, product_url, product_img, product_btn_text) VALUES ('$product_title', '$product_url','$product_img','$product_btn_text')"); 
echo "You have successfully added " .$product_title. " to your products list"; 

} else { ?> 

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
<table border="0" bordercolor="none" width="50%" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td>Product Title:</td> 
     <td><input type="text" name="title" value="product title" /></td> 
    </tr> 
    <tr> 
     <td>Product Url</td> 
     <td><input type="text" name="url" value="product url" /></td> 
    </tr> 
    <tr> 
     <td>Button text</td> 
     <td><input type="text" name="btn_text" value="Get your copy" /></td> 
    </tr> 

</table> 
    <input type="file" name="image_file" /> 



    <input type="submit" value="Submit" /> 
</form> 

<?php }?> 

的信息被添加到DATABSE,它只是將圖片上傳似乎並不奏效。

有沒有人有任何想法?謝謝大家。 :)

回答

2

像@thenetimp說的!

move_uploaded_file的目標不能是一個URL。但是必須是本地文件系統上的路徑。

$ upload_dir必須指向一個本地路徑類似

windows: c:/some/path 
unix: /some/path 
+0

對不起,我用這個:/ home/markpeth/public_html/ios/wp-content/plugins/rap/includes /它工作 – Danienllxxox 2012-02-08 21:38:48

+0

你提到我的名字但是不投票給我:-(我現在很傷心! – thenetimp 2012-02-08 21:42:07

1

你的上傳目錄是URL,它是你的問題。它需要一個本地文件系統路徑

而且,你如果有條件的檢查,如果你能寫文件有,但無奈吐出一個文本響應。你應該在那裏做你的工作,而不是在有條件的情況下。那很糟。你應該做這個。

if (file_exists($upload_dir) && is_writable($upload_dir)) { 
    echo "<br /> Directory exists and is fine.... <br />"; 
    $uploadedfile = $_FILES['image_file']['name']; 
    $extension = explode(".", $uploadedfile); 
    $extensiontype = $extension['1']; 

    $target_path = $upload_dir; 
    $target_path = $target_path .$new_name .'.'.$extensiontype; 

    if(move_uploaded_file($_FILES['image_file']['tmp_name'], $target_path)) { 
      echo "The file ". basename($_FILES['image_file']['name']). 
      " has been uploaded <br />"; 
      $product_img = $new_name.'.'.$extensiontype; 

      //ADD TO DATABASE 
      $wpdb->query("INSERT INTO wp_rec_amazon_product (product_title, product_url, product_img, product_btn_text) VALUES ('$product_title', '$product_url','$product_img','$product_btn_text')"); 
      echo "You have successfully added " .$product_title. " to your products list"; 

    } else{ 
     echo "There was an error uploading the file, please try again! <br />"; 

    } 


} 
else { 
    echo "Upload directory is not writable, or does not exist. <br />"; 
} 
+0

你是什麼意思? 。 – Danienllxxox 2012-02-08 21:35:02

+0

http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/不是本地路徑。它是一個可以在瀏覽器中訪問的URL。您需要文件系統路徑/ home/username/public_html/uploads將是一個可能的示例。 @ user914330指出你可以使用wp_upload()獲取wordpress上傳目錄的目錄路徑。 – thenetimp 2012-02-08 21:40:15

1

wp_upload_dir()

將返回WordPress的上傳目錄的本地文件路徑。

在腳本

,提出:$ upload_dir = wp_upload_dir(); 而不是你在那裏的靜態字符串。