I can save the file into wordpress but it does not show it

喜夏-厌秋 提交于 2019-12-13 07:49:28

问题


I am running wordpress on localhost, I can successfully save files into upload directory of wordpress using the following code, but wordpress does not show them in its media library, although it shows just those that I upload using its upload feature.

require("/Applications/MAMP/htdocs/wordpress/wp-load.php");

$Addr = "http://www.xxx.com" . $Addr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $Addr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

ini_set( 'display_errors', TRUE );
error_reporting( E_ALL );

$upload_dir = wp_upload_dir(); 
$location= $upload_dir['basedir']. "/" . "a.jpg";


file_put_contents($location, $output);
echo 'Photo is uploaded';

When I go to following address I can see the photos there.

http://localhost:8888/wordpress/wp-content/uploads/

I even copied a file manually but it does not show that one as well but when I upload the same file using wordpress itself the file will be shown.


回答1:


Seems a permission problem, try set 644 with chmod:

file_put_contents($location, $output);
chmod($location, 0644); // <--- new code


来源:https://stackoverflow.com/questions/14412888/i-can-save-the-file-into-wordpress-but-it-does-not-show-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!