Dear Reader, let us we know that “How to upload the file and post data using curl in PHP“. So in this Tutorial, We Learn Step By Step How to upload the file and post the data Process.
When you want to work on Web Services and API‘s and want to POST and Fetch data from another Application/Website then CURL plays a very important role for the same.
In this tutorial, I am going to aware how to post file and data from one domain to another domain. In this tutorial, I am also aware of how to store this in the database.
First, we see Snapshots for our CURL Request form using which we can post data
And you can also see Live Demo by click on bellow Demo button
Step 1:- First of all we need to create the database like “php_demo”
Step 2:- Now create a table “user” in a database using the below SQL code
1 2 3 4 5 6 7 |
CREATE TABLE `user` ( `id` int(11) NOT NULL, `name` varchar(30) NOT NULL, `email` varchar(40) NOT NULL, `photo` text NOT NULL, `created_date` datetime NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
Step 3:- Now we need to create a config.php file in the root directory
like
http://localhost/php_demo/curl/config.php
and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php define('DB_NAME','php_demo'); define('DB_USER','root'); define('DB_HOST','localhost'); define('DB_PASSWORD',''); $conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); if(!$conn) { echo 'Database not connected'; } ?> |
Step 4:- create an index.php file in our project’s root folder like “http://localhost/php_demo/curl/index.php”
and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php $message = ''; if(isset($_REQUEST['upload'])) { $ch = curl_init(); $cfile = new CURLFile( $_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name'] ); $data = array("name"=> $_REQUEST['name'],"email"=> $_REQUEST['email'],"myimage"=>$cfile); $url = "http://localhost/demo/curl/post.php"; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); $responce = curl_exec($ch); //print_r($responce); if($responce == true) { $message = '<div class = "alert alert-success">Detail updated successfully!</div>'; } else { $message = '<div class = "alert alert-danger">Some error occur!'.curl_error($ch).'</div>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>How to upload file and data using curl in php</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <div class="container"> <h2 class="alert alert-info">How to upload file and data using curl in php</h2> <?php echo $message; ?> <form action="index.php" method="POST" name="frmUpload" enctype="multipart/form-data"> <div class="form-group"> <label for="pwd">Name:</label> <input type="text" class="form-control" name="name" id="name" placeholder="Please enter name" required> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control" name="email" id="email" placeholder="Please enter email" required> </div> <div class="form-group"> <label for="pwd">Browse file:</label> <input type="file" class="form-control" name="image" id="image" required> </div> <button name="upload" type="submit" value="Upload" class="btn btn-success">Submit</button> </form> </div> </body> </html> |
Step 5:- create a post.php file in our project’s root folder like “http://localhost/php_demo/curl/post.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php include('config.php'); if(isset($_FILES['myimage']['tmp_name'])) { $path = "uploads/".$_FILES['myimage']['name']; move_uploaded_file($_FILES['myimage']['tmp_name'], $path); $sql = "INSERT INTO user(name,email,photo,created_date) VALUES('".$_REQUEST['name']."','".$_REQUEST['email']."','".$_FILES['myimage']['name']."', '".date("Y-m-d H:i:s")."')"; $result = mysqli_query($conn, $sql); if($result == 1) { $msg = '<div class="alert alert-success">Added successfully!</div>'; } else { $msg = '<div class="alert alert-danger">Some error occur, Please try again!</div>'; } } ?> |
Step 6:- Now run your project, so we need to type in browser localhost/your_project_name/index.php
like
http://localhost/php_demo/curl/index.php then you can see the result as in below snapshot, you can post data by fill detail in the form
Congratulations you have successfully created How to upload the file and post data using curl in PHP, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks.
2 Comments:
sayawismo October 19, 2018
greaaaat, thanks work very well…….
Webpreparations Team October 19, 2018
Thank you, dear Reader, for your appreciation.