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