我正在我的本地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.jpgPHP脚本返回“file_get_contents
下面是目录中的一个文件工作代码:
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);
您是否检查过目录中文件的所有文件权限? –
文件权限是可以的,因为它会上传名为file.jpg的文件,但不会是其他文件。 – user715564