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.

Share Improve this question edited May 14, 2017 at 18:39 Mayeenul Islam asked May 14, 2017 at 17:14 Mayeenul IslamMayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges 4
  • On Windows that's actually a perfectly valid file path, you can have a mix of forward and backward slashes. WordPress uses wp_normalize_path internally to fix this, but it shouldn't be an issue. – Milo Commented May 14, 2017 at 17:31
  • Can you edit your question with the code that uses wp_upload_bits and does the other parts? – Tom J Nowell Commented May 14, 2017 at 18:04
  • @TomJNowell added my code here. – Mayeenul Islam Commented May 14, 2017 at 18:39
  • @TomJNowell here is the code, it's a junk commit only to share things: github/nanodesigns/nanosupport/blob/feature/… – Mayeenul Islam Commented May 15, 2017 at 15:25
Add a comment  | 

1 Answer 1

Reset to default 1

I 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