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

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

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制