问题
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