claudios
claudios

Reputation: 6656

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.

Upvotes: 0

Views: 420

Answers (2)

JYoThI
JYoThI

Reputation: 12085

comment the return $result ..if it return means below code are not work it a general thing na

try something like 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']);
?>

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

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);
?>

Upvotes: 1

Related Questions