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

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

    点赞 评论

相关推荐