Menu

SuietASSURED Banner

Save

Save

Welcome, Guest
Username: Password: Remember me

TOPIC: file upload web service

file upload web service 1 year 6 months ago #32790

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
Hi All,

I am creating a file upload web service using rest version v4_1. The issue is I need to provide different files for uploading it in to the documents module and so I cannot give a proper path which refers to a particular file. Could you please suggest me as to how should I proceed further for the same.

I am attaching the code below


Would request you to please provide me with a solution as soon as possible.
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #32844

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
Kindly find the attached code for the above :


<?php
if(isset($_FILES)){
$errors= array();
$file_name = $_FILES;
$file_size = $_FILES;
$file_tmp = $_FILES;
$file_type = $_FILES;
$file_ext=strtolower(end(explode('.',$_FILES)));

// echo $file_tmp;


$file = file_get_contents($_FILES);
$byteArr = str_split($file);
foreach ($byteArr as $key=>$val)

{

echo $byteArr[$key] = ord($val);
$byteArr = base64_encode($byteArr);

echo $byteArr;
}




$extensions= array("jpeg","jpg","png", "docx", "doc", "pdf", "bmp","xls", "xlsx" );

if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed.";
}

if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}

if(empty($errors)==true) {
move_uploaded_file($file_tmp,"upload/".$file_name);
//echo "Success";

mysql_connect("localhost","root","") or die(mysql_error());

mysql_select_db("*******") or die(mysql_error());
for($i=0; $i<1; $i++)
{
$v = uniqid();
//$ve = uniqid();
//mysql_query("INSERT INTO document_revisions(id, document_id, filename, file_ext) VALUES ('".$v."','".$ve."', '$file_name', '$file_ext')");
// mysql_query("INSERT INTO documents(id, document_name, document_revision_id) VALUES ('".$ve."', '$file_name', '".$v."')");
mysql_query("INSERT INTO document_revisions(id, document_id, filename, file_ext) VALUES ('".$v."','".$v."', '$file_name', '$file_ext')");
mysql_query("INSERT INTO documents(id, document_name, document_revision_id) VALUES ('".$v."', '$file_name', '".$v."')");
}

}else{
print_r($errors);
}
}
?>
<html>
<body>

<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>

<ul>
<li>Sent file: <?php echo $_FILES; ?>
<li>File size: <?php echo $_FILES; ?>
<li>File type: <?php echo $_FILES ?>
</ul>

</form>

</body>
</html>
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #32846

  • item
  • item's Avatar
  • Offline
  • Elite Member
  • Posts: 317
  • Thank you received: 78
  • Karma: 21
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #32849

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
Thanks for your quick reply, but the issue is that in the above mentioned links we have to give the particular file path('file' => base64_encode(file_get_contents('/path/to/MYSpreadsheet.xlsx'))) whereas I don't want to give the real path. I need to select any of the files for uploading without giving the exact path
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #33078

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
Could anyone please help me on this. I am stuck up here from the past few months. I have tried another code as well as mentioned in the above link but it still takes the exact path of the document rather than taking paths of any of the documents. Kindly find the below code

<?php

$url = "http://{site_url}/service/v4_1/rest.php";

$username = "admin";
$password = "password";

//function to make cURL request
function call($method, $parameters, $url)
{
ob_start();
$curl_request = curl_init();

curl_setopt($curl_request, CURLOPT_URL, $url);
curl_setopt($curl_request, CURLOPT_POST, 1);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, 1);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

$jsonEncodedData = json_encode($parameters);

$post = array(
"method" => $method,
"input_type" => "JSON",
"response_type" => "JSON",
"rest_data" => $jsonEncodedData
);

curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl_request);
curl_close($curl_request);

$result = explode("\r\n\r\n", $result, 2);
$response = json_decode($result[1]);
ob_end_flush();

return $response;
}

//login
$login_parameters = array(
"user_auth" => array(
"user_name" => $username,
"password" => md5($password),
"version" => "1"
),
"application_name" => "RestTest",
"name_value_list" => array(),
);

$login_result = call("login", $login_parameters, $url);

//get session id
$session_id = $login_result->id;

//create document
$set_entry_parameters = array(
//session id
"session" => $session_id,

//The name of the module
"module_name" => "Documents",

//Record attributes
"name_value_list" => array(
//to update a record, pass in a record id as commented below
//array("name" => "id", "value" => "9b170af9-3080-e22b-fbc1-4fea74def88f"),
array("name" => "document_name", "value" => "Example Document"),
array("name" => "revision", "value" => "1"),
),
);

$set_entry_result = call("set_entry", $set_entry_parameters, $url);

echo "<pre>";
print_r($set_entry_result);
echo "</pre>";

$document_id = $set_entry_result->id;

//create document revision
$contents = file_get_contents ("/path/to/example_document.txt");

$set_document_revision_parameters = array(
//session id
"session" => $session_id,

//The attachment details
"note" => array(
//The ID of the parent document.
'id' => $document_id,

//The binary contents of the file.
'file' => base64_encode($contents),

//The name of the file
'filename' => 'example_document.txt',

//The revision number
'revision' => '1',
),
);

$set_document_revision_result = call("set_document_revision", $set_document_revision_parameters, $url);

echo "<pre>";
print_r($set_document_revision_result);
echo "</pre>";

?>

Here i am giving the exact path of the document in $contents. Can anyone help me as to how can i not give any specific path but then to the document is uploaded and can be viewed in my instance
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #33097

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
Can someone please help me out with file upload... Its very urgent and I am trying hard but its just not happening..

Please someone reply... :(
Last Edit: 1 year 6 months ago by Kinjal.
The administrator has disabled public write access.

file upload web service 1 year 6 months ago #33265

  • Kinjal
  • Kinjal's Avatar
  • Offline
  • Junior Member
  • Posts: 35
  • Thank you received: 2
  • Karma: 2
please can anyone help me out in file upload....
pleasee
The administrator has disabled public write access.

file upload web service 4 days 19 hours ago #56795

  • sam_sumit
  • sam_sumit's Avatar
  • Offline
  • New Member
  • Posts: 4
  • Karma: 0
Can you elaborate, in array 'note' referes to?

//The attachment details
"note" => array(
//The ID of the parent document.
'id' => $document_id,

//The binary contents of the file.
'file' => base64_encode($contents),

//The name of the file
'filename' => 'example_document.txt',

//The revision number
'revision' => '1',
),



I'm trying to upload image through rest API. I have a custom module with image field, how can I upload image through rest API?
The administrator has disabled public write access.
Time to create page: 0.099 seconds
Powered by Kunena Forum