admin管理员组文章数量:1390736
I'm trying to upload files into a separate directory inside the uploads/
, and I succeed using wp_upload_bits()
. I's happy with everything and I thought thing was done.
But when I's trying to delete the files, as I din't use wp_add_attachment()
etc. so I need to unlink()
the file using its path. So I stored the file array. When I's trying to use the file path, I found it's broken.
X:xampphtdocswordpress/wp-content/uploads/2017/05/filename.jpg
I was using WAMP in Windows. I tried using XAMPP. And then tried using Nginx 1.10.1 - the result is same. So I then checked the wp_upload_dir()
:
var_dump(wp_upload_dir());
Problem is it's returning:
array(6) {
["path"]=> string(52) "X:\xampp\htdocs\wordpress/wp-content/uploads/2017/05"
["url"]=> string(53) "http://localhost/wordpress/wp-content/uploads/2017/05"
["subdir"]=> string(8) "/2017/05"
["basedir"]=> string(44) "X:\xampp\htdocs\wordpress/wp-content/uploads"
["baseurl"]=> string(45) "http://localhost/wordpress/wp-content/uploads"
["error"]=> bool(false)
}
You can see the problem is in slashes - a bad combination of forward (/
) and back slashes (\
) are there. And I guess that's what causing the issue.
I know I can make up the file path using WP_CONTENT_DIR
and some hard coded strings, but I want to stick with the dynamic features as much as possible.
What can I do now?
EDIT
I did quite a lot of works here. BTW I think only this part is the key:
$attachment_file = wp_upload_bits( $_FILES['my_attachment']['name'][$key], null, file_get_contents($_FILES['my_attachment']['tmp_name'][$key]) );
The $key
because there are multiple files and doing the same for each of 'em.
I'm trying to upload files into a separate directory inside the uploads/
, and I succeed using wp_upload_bits()
. I's happy with everything and I thought thing was done.
But when I's trying to delete the files, as I din't use wp_add_attachment()
etc. so I need to unlink()
the file using its path. So I stored the file array. When I's trying to use the file path, I found it's broken.
X:xampphtdocswordpress/wp-content/uploads/2017/05/filename.jpg
I was using WAMP in Windows. I tried using XAMPP. And then tried using Nginx 1.10.1 - the result is same. So I then checked the wp_upload_dir()
:
var_dump(wp_upload_dir());
Problem is it's returning:
array(6) {
["path"]=> string(52) "X:\xampp\htdocs\wordpress/wp-content/uploads/2017/05"
["url"]=> string(53) "http://localhost/wordpress/wp-content/uploads/2017/05"
["subdir"]=> string(8) "/2017/05"
["basedir"]=> string(44) "X:\xampp\htdocs\wordpress/wp-content/uploads"
["baseurl"]=> string(45) "http://localhost/wordpress/wp-content/uploads"
["error"]=> bool(false)
}
You can see the problem is in slashes - a bad combination of forward (/
) and back slashes (\
) are there. And I guess that's what causing the issue.
I know I can make up the file path using WP_CONTENT_DIR
and some hard coded strings, but I want to stick with the dynamic features as much as possible.
What can I do now?
EDIT
I did quite a lot of works here. BTW I think only this part is the key:
$attachment_file = wp_upload_bits( $_FILES['my_attachment']['name'][$key], null, file_get_contents($_FILES['my_attachment']['tmp_name'][$key]) );
The $key
because there are multiple files and doing the same for each of 'em.
1 Answer
Reset to default 1I ran into the same exact issue (getting paths like 'X:xampphtdocswordpress/wp-content/uploads/2017/05/filename.jpg' on WAMP on Windows). The issue seems to be due to update_post_meta() passing values through stripslashes() when storing the data.
The workaround is to add wp_slash() around your value you pass to update_post_meta().
You're probably calling update_post_meta() something like this:
update_post_meta( $id, 'doesnt_work', $data );
You need to add wp_slash() to the data before or as you call update_post_meta()
update_post_meta( $id, 'does_work', wp_slash( $data ) );
The end result:
array ( 'file' => 'C:\\wamp64\\www\\wpdev\\wpdev_test/wp-content/uploads/2017/08/filename', ....
本文标签: uploadswpuploadbits() is not giving the file path right in localhost
版权声明:本文标题:uploads - wp_upload_bits() is not giving the file path right in localhost 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744754062a2623354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_normalize_path
internally to fix this, but it shouldn't be an issue. – Milo Commented May 14, 2017 at 17:31wp_upload_bits
and does the other parts? – Tom J Nowell ♦ Commented May 14, 2017 at 18:04