dsfdsf48652 2013-12-13 17:01
浏览 50
已采纳

PHP脚本返回“file_get_contents

I am building a site on my local Windows 8 dev machine(using wamp). I have a PHP script that is suppose to take image files from a directory on my server and upload them to a WordPress based website. When I have one file in the directory everything works fine. When I modify the code to upload multiple files from the directory, only one image is uploaded and I get an error saying: file_get_content(/path/to/directory/): failed ot open stream. Permission denied in /path/to/script/ Here is an image of the error I am receiving http://i.imgur.com/rRvxKa0.jpg

Here is the code that works with one file in the directory:

    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);

Here is the code that is suppose to upload multiple images.

    $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); 
  • 写回答

1条回答 默认 最新

  • doudian7996 2013-12-13 18:26
    关注

    scandir will return . and .., along with all real files and subdirectories. You need to filter those out:

    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);
    }
    

    The tip-off here is the error message: file_get_contents(/path/to/directory/): failed to open stream. Permission denied in /path/to/script/ If $file was a real file, you would see /path/to/directory/somefile.extension, not just /path/to/directory/.

    Also, you are giving every uploaded file the same file name with this line: $filename = "remote_filename.jpg"; So, no matter how many files are uploaded, only one is left at the end (the first one, because of 'overwrite'=>false).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载