PHP 图片上传(WordPress 自定义插件)
我正在开发一个 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 }?>
信息被添加到数据库中,只是图像上传似乎不起作用。
有人有什么想法吗?预先感谢大家。 :)
I have a WordPress plug in that I am working on and I seem to have hit a problem.
I need the user to be able to upload an image, instead I keep getting this problem:
Upload directory is not writable, or does not exist.
Warning:
move_uploaded_file(http://markpetherbridge.co.uk/ios/wp-content/plugins/rap/includes/productimages/78.105.162.431328735863.png)
[function.move-uploaded-file]: failed to open stream: HTTP wrapper
does not support writeable connections in
/home/markpeth/public_html/ios/wp-content/plugins/rap/includes/products.php
on line 39
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move '/tmp/phpowqMlu' to
'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
on line 39 There was an error uploading the file, please try again!
You have successfully added test_title to your products list
The directory definitely exists and I even changed the CHMOD to 777 for a test.
The products.php file looks like this:
<?
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 }?>
The information gets added to the databse, its just the image upload that does not seem to be working.
Has anyone got any ideas? thank you all in advance. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像@thenetimp 说的!
move_uploaded_file 的目标不能是 URL。但必须是本地文件系统上的路径。
$upload_dir 必须指向本地路径,例如
Like @thenetimp said!
The destination of move_uploaded_file cannot be a URL. But instead has to be a path on the local file system.
$upload_dir has to point to a local path like
wp_upload_dir()
将返回 WordPress 中上传目录的本地文件路径。
在您的脚本中,输入: $upload_dir = wp_upload_dir();
而不是你那里的静态字符串。
wp_upload_dir()
will return the local file path of your upload directory in WordPress.
in your script, put : $upload_dir = wp_upload_dir();
instead of the static string you have there.
您的上传目录是 URL 这是您的问题。它需要是本地文件系统路径
此外,您有一个 if 条件来检查是否可以写入文件,但除了输出文本响应之外什么也不做。你应该在那里做你的工作,而不是在 if 条件下面。那很糟糕。你应该这样做。
Your upload dir is URL That is your problem. It needs to be a local filesystem path
Also, you have an if conditional to check if you can write the file, but do nothing but spit out a text response. You should be doing your work in there, instead it's below the if conditional. That's bad. YOu should do this.