claudios claudios - 6 months ago 22
JSON Question

Return json for uploaded filename

I am in the midst of modifying a code from Jquery File Upload Plugin from Hayageek. Everything works fine when using the original code but when I modified it for the sake of inserting the filename uploaded into DB the json won't return the filename.

Original code:

<?php
$fileName = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
$ret[]= $fileName;

echo json_encode($ret);
?>


Modified code:

<?php
$data = array(
'filename' => $_FILES["myfile"]["name"],
'checklist_item_id' => 2, // temporary checklist_item_id
'date_uploaded' => date('Y-m-d h:i:s'),
'uploaded_by' => $this->ion_auth->get_user_id()
);
$result = $this->doc_item->insert($data);
return $result;

move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
$ret[]= $fileName;
echo json_encode($data['filename']);
?>



Modified code works okay too. Everything was inserted into database
including filename but it won't display json encode.

Answer

In order to have a similar thing with the original code returned by your modified code, use this:

<?php 
$data = array(
  'filename'          => $_FILES["myfile"]["name"],
  'checklist_item_id' => 2, // temporary checklist_item_id
  'date_uploaded'     => date('Y-m-d h:i:s'),
  'uploaded_by'       => $this->ion_auth->get_user_id()
  );
$result = $this->doc_item->insert($data);
//return $result;

move_uploaded_file($_FILES["myfile"]["tmp_name"], $output_dir.$fileName);
$ret[]= $fileName;
//echo json_encode($data['filename']);
echo json_encode($ret);
?>