我正在尝试将文件上载到uploads/, 我成功地使用wp_upload_bits(). 我对一切都很满意,我认为事情已经完成了。
但当我试图删除文件时din\'t use wp_add_attachment() 等等,所以我需要unlink() 使用其路径的文件。所以我存储了文件数组。当我试图使用文件路径时,我发现它已损坏。
X:xampphtdocswordpress/wp-content/uploads/2017/05/filename.jpg
 我在Windows中使用WAMP。我尝试使用XAMPP。然后尝试使用Nginx 1.10.1-结果相同。然后我检查了
wp_upload_dir():
var_dump(wp_upload_dir());
 问题是它正在返回:
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)
}
 你可以看到问题出在斜杠上,斜杠是前进的错误组合(
/) 和后斜杠(
\\) 在那里。我想这就是问题的根源。
我知道我可以使用WP_CONTENT_DIR 和一些硬编码字符串,但我希望尽可能多地使用动态特性。
我现在能做什么?
编辑我在这里做了很多工作。顺便说一句,我认为只有这一部分才是关键:
$attachment_file = wp_upload_bits( $_FILES[\'my_attachment\'][\'name\'][$key], null, file_get_contents($_FILES[\'my_attachment\'][\'tmp_name\'][$key]) );
 The
$key 因为有多个文件,并且对每个文件都执行相同的操作。
 
                    最合适的回答,由SO网友:Jason Rush 整理而成
                    我遇到了同样的问题(在Windows上的WAMP上获得类似“X:xampphtdocswordpress/wp-content/uploads/2017/05/filename.jpg”的路径)。问题似乎是由于update_post_meta() passing values through stripslashes() 存储数据时。
解决方法是在传递给update\\u post\\u meta()的值周围添加wp\\u slash()。
您可能正在调用update\\u post\\u meta(),如下所示:
update_post_meta( $id, \'doesnt_work\', $data );
 您需要在update\\u post\\u meta()之前或调用update\\u post\\u meta()时向数据中添加wp\\u slash()
update_post_meta( $id, \'does_work\', wp_slash( $data ) );
 最终结果:
array ( \'file\' => \'C:\\\\wamp64\\\\www\\\\wpdev\\\\wpdev_test/wp-content/uploads/2017/08/filename\', ....