php - ajaxfileupload issue with CodeIgniter -
i working on uploading image asynchronously , got this tutorial. read , tried remove model don't need , make according needs, when run no error , file not uploaded either.
i put 1 echo in controller upload function, don't echo string either, though put full path in ajaxfileupload url.
here view:
<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script src="<?php echo site_url()?>public/assets/js/ajaxfileupload.js"></script> <script> $(function() { $('#upload_file').submit(function(e) { e.preventdefault(); $.ajaxfileupload({ url : 'upload2/upload_file', secureuri : false, fileelementid: 'userfile', datatype : 'json', data : { 'title': $('#title').val() }, success : function (data, status) { if(data.status != 'error') { $('#files').html('<p>reloading files...</p>'); refresh_files(); $('#title').val(''); } alert(data.msg); } }); return false; }); }); </script> </head> <body> <h1>upload file</h1> <form method="post" action="" id="upload_file"> <label for="title">title</label> <input type="text" name="title" id="title" value="" /> <label for="userfile">file</label> <input type="file" name="userfile" id="userfile" size="20" /> <input type="submit" name="submit" id="submit" /> </form> <h2>files</h2> <div id="files"></div> </body> </html>
and controller this:
<?php class upload2 extends ci_controller { public function __construct() { parent::__construct(); $this->load->helper('url'); } public function index() { $this->load->view('upload'); } public function upload_file() { /* it's not showing me echo , path ok too, , put whole path no result. */ echo "in upload_file"; $status = ""; $msg = ""; $file_element_name = 'userfile'; if (empty($_post['title'])) { $status = "error"; $msg = "please enter title"; } if ($status != "error") { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|doc|txt'; $config['max_size'] = 1024 * 8; $config['encrypt_name'] = true; $this->load->library('upload', $config); if (!$this->upload->do_upload($file_element_name)) { $status = 'error'; $msg = $this->upload->display_errors('', ''); } else { $data = $this->upload->data(); $file_id = $this->files_model->insert_file($data['file_name'], $_post['title']); if($file_id) { $status = "success"; $msg = "file uploaded"; } else { unlink($data['full_path']); $status = "error"; $msg = "something went wrong when saving file, please try again."; } } @unlink($_files[$file_element_name]); } echo json_encode(array('status' => $status, 'msg' => $msg)); } } ?>
add attribute form enctype="multipart/form-data"
Comments
Post a Comment