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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试