File Transfer Using Curl
The Form File from where the Curl Request is Sent
<?php
if($_POST){
echo "<pre>";
print_r($_POST);
echo "</pre>";
$localfile = $_FILES['txtUpload']['tmp_name'];
$transFile = chunk_split(base64_encode(file_get_contents($localfile)));
$_POST['txtUpload'] = $transFile ;
$_POST['fileName'] = str_replace(" ", "_",$_FILES['txtUpload']['name']);
//$curlData = array_merge($curlData, array("file"=>"@".$_POST['txtUpload'].$_POST['fileName']));
$curlData = $_POST;
$postCurlStr = '';
foreach($curlData as $key=>$val){
$postCurlStr .= $key.'='.urlencode($val).'&';
// $postCurlStr .= $key.'='.$val.'&';
}
$postCurlStr = substr($postCurlStr, 0, -1);
echo "<br/>".$postCurlStr."</br/>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, 'http://biraj.dev/biraj-practice/curl/curlpage.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postCurlStr);
//$postresponse = curl_exec ($ch);
$data = curl_exec($ch);
curl_close($ch);
}
?>
<html>
<head>
<title>Curl Post</title>
<style type="text/css">
body{
background-color: #FFFFFF;
font-family:"Verdana";
margin: 10px;
padding: 0px;
color: #222222;
font-size:10px;
}
td{
height:50px;
vertical-align:top;
font-size:12px;
}
input{
font-size:12px;
}
</style>
</head>
<body>
<h2>Post</h2>
<form name="frmCurlForm" method="post" enctype="multipart/form-data" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0" width="80%">
<tr>
<td width="40%">Name ::</td>
<td width="60%"><input type="text" name="txtName"></td>
</tr>
<tr>
<td>Description ::</td>
<td><textarea name="txtDescription"></textarea></td>
</tr>
<tr>
<td>Status</td>
<td><input type="radio" name="rdoStatus" value="1">Active <input type="radio" name="rdoStatus" value="0">Inactive</td>
</tr>
<tr>
<td>Category</td>
<td><input type="checkbox" name="chkCategory" value="ES">Equity Share <input type="checkbox" name="chkCategory" value="D">Debenture</td>
</tr>
<tr>
<td>Mode</td>
<td><select name="cmbMode">
<option value="Publish">Publish</option>
<option value="Draft">Draft</option>
</select>
</td>
</tr>
<tr>
<td>Attachment</td>
<td><input type="file" name="txtUpload"></td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" value="Go"></td>
</tr>
</table>
</form>
</body>
</html>
The File that exists in the Server. [receives curl request]
<?php
$host = "localhost";
$username = "root";
$password = "336677";
$db = "test";
mysql_connect($host, $username, $password) or die("Hostname not found. ".mysql_error());
mysql_select_db($db) or die("Database not found. ".mysql_error());
$name = $_REQUEST["txtName"];
$description = $_REQUEST["txtDescription"];
$status = $_REQUEST["rdoStatus"];
$category = $_REQUEST["chkCategory"];
$mode = $_REQUEST["cmbMode"];
$query = "INSERT INTO tb_curl (name, description, status, category, mode) VALUES('".$name."', '".$description ."', '".$status ."', '".$category."', '".$mode."')";
mysql_query($query) or die(mysql_error());
$img = base64_decode($_POST['txtUpload']);
$fileName = $_POST['fileName'];
//$fp = "uploadfiles/".$fileName;
$fp = $fileName;
$handle = fopen($fp,"w+");
fwrite($handle,$img);
?>
No comments:
Post a Comment