I need help guys, I have some api to upload a file.But, i haven't experience adout it.
What i do first is create a view and controller to upload my file to a folder,here my code:
View
<form role="form" action="<?php echo site_url('stepfour/adduploadExcel') ?>" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<label for="exampleInputFile"><a href="#">Excel Template Format Sample</a></label>
<input type="file" id="exampleInputFile" name="product_benefit_data">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</div>
</div>
</form>
and here my controller look like:
public function adduploadExcel()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('product_benefit_data')){
redirect('stepthree/viewstepthree');
}else{
$upload_data = $this->upload->data();
$data = array (
"product_id" => $this->input->post('product_id'),
"currency_id" => $this->input->post('codeexclusion')
);
$jsonData = json_encode($data);
$this->uploadApi($jsonData);
// redirect('stepfour/viewstepfour');
}
}
and this api that i want to hit using php curl:
public function uploadApi($jsonData)
{
$sessionData = $this->session->userdata('logged_in');
$accessToken = $sessionData['access_token'];
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$accessToken
);
$uploadDir = base_url('upload/');
$uploadFile = $uploadDir . basename($_FILES['product_benefit_data']['name']);
if (move_uploaded_file($_FILES['product_benefit_data']['tmp_name'], $uploadFile))
{
$uploadRequest = array(
'fileName' => basename($uploadFile),
'fileData' => base64_encode(fopen($uploadFile))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://development.dev/api/product');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$curl_exec = curl_exec($ch);
$result = json_decode($curl_exec, TRUE);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode == 200)
{
$msg['success'] = 'Upload Success!';
$this->load->view('back/vheader');
$this->load->view('back/product/vstepfour',$msg);
$this->load->view('back/vfooter');
} elseif ($httpcode == 500) {
$msg['failed'] = 'Upload Fail!';
$this->load->view('back/vheader');
$this->load->view('back/product/vstepfour',$msg);
$this->load->view('back/vfooter');
}
}else{
echo "Possible file upload attack!\n";
}
}
I learn using this step look like this thread, but i got error like this:
Severity: Warning
Message: move_uploaded_file(http://personal-web/devebb/upload/Business_Flow.xlsx): failed to open stream: HTTP wrapper does not support writeable connections
Filename: controllers/Stepfour.php
Line Number: 119
Backtrace:
File: C:\xampp\htdocs\personal\devebb\application\controllers\Stepfour.php Line: 119 Function: move_uploaded_file
File: C:\xampp\htdocs\personal\devebb\application\controllers\Stepfour.php Line: 101 Function: uploadApi
File: C:\xampp\htdocs\personal\devebb\index.php Line: 315 Function: require_once