Skip to content

Instantly share code, notes, and snippets.

@tylerreed
Created November 25, 2011 01:16
Show Gist options
  • Save tylerreed/1392601 to your computer and use it in GitHub Desktop.
Save tylerreed/1392601 to your computer and use it in GitHub Desktop.
Valums AJAX File Uploader for CodeIgniter
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_Controller extends CI_Controller {
public function upload_files()
{
if ($this->input->get('qqfile'))
{
$file = $this->input->get('qqfile');
}
else if ($_FILES['qqfile'])
{
$file = $_FILES['qqfile']['tmp_name'];
}
if ($this->security->xss_clean($file))
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
$extensions = array('png', 'jpeg', 'jpg', 'gif', 'bmp'); // Acceptable file extensions
if (in_array($ext, $extensions))
{
$directory = './uploads/';
if ($this->input->get('qqfile'))
{
$input = fopen('php://input', 'r');
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($directory . $filename, 'w');
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
}
else if ($_FILES['qqfile'])
{
move_uploaded_file($_FILES['qqfile']['tmp_name'], $directory . $filename.'.'.$ext);
}
$this->output->set_output(json_encode(array('success' => 'true', 'filename' => $file)));
}
else
{
$this->output->set_output(json_encode(array('error' => 'File type not supported.')));
}
}
?>
@tylerreed
Copy link
Author

I have excluded file size checking in the snippet - I still need to add that at some point. I have excluded the code where I change the name of the file and also create thumbnail images - but one could easily add that onto the snippet I have shared.

@pushpinderbagga
Copy link

Saved me a ton of time. Thanks a lot!

@tylerreed
Copy link
Author

@pushpinderbagga Great! :)

@izdrail
Copy link

izdrail commented Apr 1, 2014

hy the path is server path or script path ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment