我正在我的本地Windows 8开发机器上构建一个站点(使用wamp)。我有一个PHP脚本,假设从我服务器上的目录中获取图像文件并将它们上传到基于WordPress的网站。当我在目录中有一个文件时,一切正常。当我修改代码以从目录上传多个文件时,只上传了一个图片,我收到错误消息:file_get_content(/path/to/directory/): failed ot open stream. Permission denied in /path/to/script/
这是我收到的错误的图片http://i.imgur.com/rRvxKa0.jpg
以下代码适用于目录中的一个文件:
global $post;
$rpcurl = get_bloginfo('url') . "/xmlrpc.php";
$username = 'admin';
$password = 'admin';
$blogid = $post->ID; //Post ID
$post_idn = get_post_meta($post->ID, 'VIN', true);
$post_dir = dirname( get_template_directory() ) . "/" . $post_idn;
$file = file_get_contents( $post_dir . "/file.jpg");
$filetype = "image/jpeg";
$filename = "remote_filename.jpg";
xmlrpc_set_type($file,'base64'); // <-- required!
$params = array($blogid,$username,$password,
array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request = xmlrpc_encode_request('wp.uploadFile',$params);
function go($request,$rpcurl){
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,$rpcurl);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
}
$result = go($request,$rpcurl);
print_r($result);
以下是上传多张图片的代码。
$rpcurl = get_bloginfo('url') . "/xmlrpc.php";
echo $rpcurl;
$username = 'admin';
$password = 'admin';
$blogid = $post->ID; //Post ID
echo $blogid;
$post_idn = get_post_meta($post->ID, 'VIN', true);
echo "<h1>" . $post_idn ."</h1>";
$post_dir = dirname( get_template_directory() ) . "/" . $post_idn;
$fileslist = scandir($post_dir);
foreach ($fileslist as $file) {
$file = file_get_contents( $post_dir . "/" . $file);
$filetype = "image/jpeg";
$filename = "remote_filename.jpg";
xmlrpc_set_type($file,'base64'); // <-- required!
$params = array($blogid,$username,$password,
array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request = xmlrpc_encode_request('wp.uploadFile',$params);
}
function go($request,$rpcurl){
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,$rpcurl);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
}
$result = go($request,$rpcurl);
print_r($result);
答案 0 :(得分:1)
scandir
will return .
and ..
, along with all real files and subdirectories。你需要过滤掉那些:
foreach ($fileslist as $file) {
if( $file === '.' || $file === '..' ) {
continue;
}
$file = file_get_contents( $post_dir . "/" . $file);
$filetype = "image/jpeg";
$filename = "remote_filename.jpg";
xmlrpc_set_type($file,'base64'); // <-- required!
$params = array($blogid,$username,$password,
array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request = xmlrpc_encode_request('wp.uploadFile',$params);
}
此处的提示是错误消息:file_get_contents(/path/to/directory/): failed to open stream. Permission denied in /path/to/script/
如果$file
是真实文件,您会看到/path/to/directory/somefile.extension
,而不只是/path/to/directory/
。
此外,您使用以下行为每个上传的文件指定相同的文件名:$filename = "remote_filename.jpg";
因此,无论上传了多少文件,最后只留下一个(第一个,因为{{ 1}})。