curl using post data in php

curl using post data in php

index.php
---------


<?php

if(isset($_FILES['files']))
{
    $errors= array();
   
        $file_name =$_FILES['files']['name'];
        $file_size =$_FILES['files']['size'];
        $file_tmp =$_FILES['files']['tmp_name'];
        $file_type=$_FILES['files']['type'];
            //9 mb something file alloweded
        if($file_size > 9097152){
            $errors[]='File size must be less than 2 MB';
        }

    if(empty($error)){
        //this is a url post data in files
        $service_url = 'http://localhost/api/index.php/test';
       $curl = curl_init($service_url);
       //POST data
       $curl_post_data = array(
            "file_name" =>$file_name,
            "file_size" =>$file_size,
            "file_tmp" =>$file_tmp,
            "file_type" =>$file_type
            );
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_POST, true);
       curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
       $curl_response = curl_exec($curl);
       curl_close($curl);

        print_r($curl_response); 
        echo "Success";
    }   
}
?>
<!--create a form for file -->
<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="files" />
    <input type="submit"/>
</form>


api/function.php
----------------
<?php
function test()
{
$file_name=$_POST[file_name];
$file_size=$_POST[file_size];
$file_type=$_POST[file_type];
$file_tmp=$_POST[file_tmp];

$sql = "INSERT into upload_data (`fid`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('','$file_name','$file_size','$file_type')";

        $db = getConnection();
        $sqlQQ =  $db->prepare($sql);
        $sqlQQ->execute();   
        $desired_dir="user_data";
       
        if(empty($errors)==true)
        {
               copy($file_tmp,"user_data/". $file_name );
        }
        else
        {
                print_r($errors);
        }

}
?>

-----------------
api/index.php
-----------------
$app->post('/test', 'test');
----------------------------
Share on Google Plus

About quora Questions & Answers A-Z

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment