• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
uploading/inserting images problem

#1
[eluser]cerberus478[/eluser]
Hi

I was busy doing a tutorial called How to Upload Files with CodeIgniter and AJAX, I did everything like it said but it's not uploading or saving the content.

this is the upload controller
Code:
<?php

class Upload extends CI_Controller
{

public function __construct()
{
  parent::__construct();
  $this->load->model('files_model');
  $this->load->database();
  $this->load->helper('url');
}

public function index()
{
  $this->load->view('upload');
}

public function upload_file()
{
  $status = "";
  $msg = "";
  $file_element_name = 'userfile';
  
  if (empty($_POST['title']))
  {
   $status = "error";
   $msg = "Please enter a title";
  }
  
  if ($status != "error")
  {
   $config['upload_path'] = './files/';
   $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 successfully uploaded";
    }
    else
    {
     unlink($data['full_path']);
     $status = "error";
     $msg = "Something went wrong when saving the file, please try again.";
    }
   }
   @unlink($_FILES[$file_element_name]);
  }
  echo json_encode(array('status' => $status, 'msg' => $msg));
}

public function files()
{
  $files = $this->files_model->get_files();
  $this->load->view('files', array('files' => $files));
}

public function delete_file($file_id)
{
  if ($this->files_model->delete_file($file_id))
  {
   $status = 'success';
   $msg = 'File successfully deleted';
  }
  else
  {
   $status = 'error';
   $msg = 'Something went wrong when deleteing the file, please try again';
  }
  echo json_encode(array('status' => $status, 'msg' => $msg));
}

}

This is my files_model
Code:
<?php

class Files_Model extends CI_Model {

public function insert_file($filename, $title)
{
  $data = array(
   'filename'  => $filename,
   'title'   => $title
  );
  $this->db->insert('files', $data);
  return $this->db->insert_id();
}

public function delete_file($file_id)
{
  $file = $this->get_file($file_id);
  if (!$this->db->where('id', $file_id)->delete('files'))
  {
   return FALSE;
  }
  unlink('./files/' . $file->filename);
  return TRUE;
}

public function get_files()
{
  return $this->db->select()
    ->from('files')
    ->get()
    ->result();
}

public function get_file($file_id)
{
  return $this->db->select()
    ->from('files')
    ->where('id', $file_id)
    ->get()
    ->row();
}

}

This is my upload view
Code:
<!doctype html>
&lt;html&gt;
&lt;head&gt;
[removed][removed]
[removed][removed]
[removed][removed]
&lt;link href="&lt;?php echo base_url()?&gt;css/style.css" rel="stylesheet" /&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Upload File</h1>
<p class="error"></p>
&lt;form method="post" action="&lt;?php echo site_url('/upload/upload')?&gt;" id="upload_file"&gt;
  <label for="title">Title</label>
  <br />
  &lt;input type="text" name="title" id="title" value="" /&gt;
  <br />
  <label for="userfile">File</label>
  <br />
  &lt;input type="file" name="userfile" id="userfile" size="20" /&gt;
  <br />
  &lt;input type="submit" name="submit" id="submit" /&gt;
&lt;/form&gt;
<h2>Files</h2>
<div id="files"></div>
&lt;/body&gt;
&lt;/html&gt;

This is my files view
Code:
&lt;?php
if (isset($files) && count($files))
{
?&gt;
  <ul>
   &lt;?php
   foreach ($files as $file)
   {
    ?&gt;
    <li class="image_wrap">
     <a href="#" class="delete_file_link"id="&lt;?php echo $file-&gt;id?&gt;">Delete</a>
     <strong>&lt;?php echo $file->title?&gt;</strong>
     <br />
     &lt;?php echo $file->filename?&gt;
    </li>
    &lt;?php
   }
   ?&gt;
  </ul>
&lt;/form&gt;
&lt;?php
}
else
{
?&gt;
<p>No Files Uploaded</p>
&lt;?php
}
?&gt;

this is site.js
Code:
$(function() {
$('#upload_file').submit(function(e) {
  e.preventDefault();
  $.ajaxFileUpload({
   url    :'./upload/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;
});

refresh_files();

$('.delete_file_link').live('click', function(e) {
  e.preventDefault();
  if (confirm('Are you sure you want to delete this file?'))
  {
   var link = $(this);
   $.ajax({
    url   : './upload/delete_file/' + link.data('file_id'),
    dataType : 'json',
    success  : function (data)
    {
     if (data.status == "success")
     {
      link.parents('li').fadeOut('fast', function() {
       $(this).remove();
       if ($('#files li').length == 0)
       {
        $('#files').html('<p>No Files Uploaded</p>');
       }
      });
     }
     else
     {
      alert(data.msg);
     }
    }
   });
  }
});
});

function refresh_files()
{
$.get('./upload/files/')
.success(function (data){
  $('#files').html(data);
});
}


#2
[eluser]cerberus478[/eluser]
I also have ajaxfileupload.js but it seems to big for the post and I can't attach it


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.