PHP 图片上传(WordPress 自定义插件)

3

我正在开发一个WordPress插件,但似乎遇到了问题。 我需要用户能够上传图像,但我一直遇到这个问题:

上传目录不可写或不存在。

警告: move_uploaded_file(http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png) [function.move-uploaded-file]:无法打开流:HTTP包装器 不支持可写连接,在 /home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php 的第39行


警告:move_uploaded_file() [function.move-uploaded-file]:无法将'/tmp/phpowqMlu'移动到'http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png' 在/home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php的第39行中有错误上传文件,请重试! 您已成功将test_title添加到产品列表中


该目录肯定存在,我甚至为测试更改了CHMOD为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 }?>

信息已添加到数据库,只是图片上传似乎无法正常工作。

有人有什么想法吗?非常感谢大家。:)

3个回答

2

就像 @thenetimp 所说的一样!

move_uploaded_file 的目标不能是一个 URL,而必须是本地文件系统上的路径。

$upload_dir 必须指向一个本地路径,如

windows: c:/some/path
unix: /some/path

抱歉,我使用了这个路径:/home/markpeth/public_html/ios/wp-content/plugins/rap/includes/,并且它有效了。 - Danienllxxox

1

你的上传目录是URL,这就是你的问题。它需要是本地文件系统路径。

另外,你有一个if条件语句来检查是否可以写入文件,但除了输出文本响应之外什么也没做。你应该在其中完成你的工作,而不是放在if条件语句下面。这很糟糕。你应该这样做。

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 />";
}

http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/ 不是本地路径,而是可以在浏览器中访问的URL。您需要文件系统路径,例如/home/username/public_html/uploads。@user914330指出,您可以使用wp_upload()来获取wordpress上传目录的目录路径。 - thenetimp

1

wp_upload_dir()

将返回WordPress上传目录的本地文件路径。

在您的脚本中,使用$upload_dir = wp_upload_dir();代替您当前使用的静态字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接