Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP脚本返回";文件\u获取\u内容_Php_Wordpress_Xml Rpc - Fatal编程技术网

PHP脚本返回";文件\u获取\u内容

PHP脚本返回";文件\u获取\u内容,php,wordpress,xml-rpc,Php,Wordpress,Xml Rpc,我正在本地Windows8开发机器上构建一个站点(使用wamp)。我有一个PHP脚本,可以从我的服务器上的目录中获取图像文件,并将它们上传到基于WordPress的网站。当目录中有一个文件时,一切正常。当我修改代码以从目录上载多个文件时,只上载了一个图像,并且我得到一个错误消息:file\u get\u content(/path/to/directory/):打开流失败。在/path/to/script/中被拒绝的权限下面是我收到的错误的图像 以下是使用目录中的一个文件的代码: glo

我正在本地Windows8开发机器上构建一个站点(使用wamp)。我有一个PHP脚本,可以从我的服务器上的目录中获取图像文件,并将它们上传到基于WordPress的网站。当目录中有一个文件时,一切正常。当我修改代码以从目录上载多个文件时,只上载了一个图像,并且我得到一个错误消息:
file\u get\u content(/path/to/directory/):打开流失败。在/path/to/script/
中被拒绝的权限下面是我收到的错误的图像

以下是使用目录中的一个文件的代码:

    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);
global$post;
$rpcurl=get_bloginfo('url')。“/xmlrpc.php”;
$username='admin';
$password='admin';
$blogid=$post->ID//邮政ID
$post\u idn=get\u post\u meta($post->ID,'VIN',true);
$post\u dir=dirname(获取模板目录()。"/" . $后idn;
$file=file\u get\u contents($post\u dir./file.jpg”);
$filetype=“image/jpeg”;
$filename=“remote_filename.jpg”;
xmlrpc_set_type($file,'base64');//$文件名,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request=xmlrpc_encode_请求('wp.uploadFile',$params);
函数go($request,$rpcurl){
$ch=curl_init();
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_URL,$rpcurl);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl\u exec($ch);
}
$result=go($request,$rpcurl);
打印(结果);
下面是假定上载多个图像的代码

    $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); 
$rpcurl=get_bloginfo('url')。“/xmlrpc.php”;
echo$rpcurl;
$username='admin';
$password='admin';
$blogid=$post->ID//邮政ID
echo$blogid;
$post\u idn=get\u post\u meta($post->ID,'VIN',true);
“回声”$邮政署;
$post\u dir=dirname(获取模板目录()。"/" . $后idn;
$fileslist=scandir($post_dir);
foreach($fileslistas$file){
$file=文件内容($post\u dir./“$file);
$filetype=“image/jpeg”;
$filename=“remote_filename.jpg”;
xmlrpc_set_type($file,'base64');/$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request=xmlrpc_encode_请求('wp.uploadFile',$params);
}
函数go($request,$rpcurl){
$ch=curl_init();
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_URL,$rpcurl);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl\u exec($ch);
}
$result=go($request,$rpcurl);
打印(结果);
。您需要过滤掉这些内容:

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);
}
foreach($fileslistas$file){
如果($file=='.| |$file==''.'){
继续;
}
$file=文件内容($post\u dir./“$file);
$filetype=“image/jpeg”;
$filename=“remote_filename.jpg”;
xmlrpc_set_type($file,'base64');/$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request=xmlrpc_encode_请求('wp.uploadFile',$params);
}
这里的提示是错误消息:
file\u get\u contents(/path/to/directory/):无法打开流。在/path/to/script/
中被拒绝的权限如果
$file
是真实文件,您将看到
/path/to/directory/somefile.extension
,而不仅仅是
/path/to/directory/


此外,您在以下行中为每个上载的文件指定了相同的文件名:
$filename=“remote\u filename.jpg”
因此,无论上传了多少文件,最后只剩下一个文件(第一个,因为
'overwrite'=>false
)。

您检查了目录中文件的所有文件权限了吗?文件权限没有问题,因为它将上载名为file.jpg的文件,但不会上载任何其他文件。谢谢您的帮助。实际上我刚刚注意到添加了“.”和“.”。这确实删除了错误,但仍只有一个文件正在上载。我想这与foreach语句有关。请参阅我上面的最后一段(刚刚添加)——您为每个文件指定了相同的名称,因此只保存了一个文件。