-
-
Save cam-gists/3107574 to your computer and use it in GitHub Desktop.
CodeIgniter: PHP: AJAX File Uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.'))); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment