The best solution for integrating codeigniter with swfupload

  • 2021-07-01 06:44:28
  • OfStack

codeigniter is a lightweight and convenient MVC framework. The recent project involves batch uploading, so swfupload is used as a plug-in. Although there are many posts about ci and swfupload on the Internet, they are not very complete. Therefore, I have made the following arrangement by integrating the advantages of each family and my own practical experience.

Question 1: Can't get the file type?

Answer: Since MIME of all types of SWFUPLOAD files are application/octet-stream, it is impossible to accurately obtain whether it is a picture by using the upload class provided by Codeigniter. According to the official wiki of ci, the answer has been given

In your view file, include the SWFUpload javascript file:

<script type="text/javascript" src="jscripts/SWFUpload/mmSWFUpload.js"></script> 
In your controller, pass  " Filedata "  as the name value for the do_upload function:
$this->upload->do_upload('Filedata') 
In your mimes.php file, add  ' application/octet-stream' for all allowed image formats
'gif'    =>    array('image/gif', 'application/octet-stream'),
'jpeg'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpg'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'jpe'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
'png'    =>    array('image/png',  'image/x-png', 'application/octet-stream'),

Problem 2: The background with session authentication will lead to the condition of exiting login after uploading

Answer:

1 general method: When swfuplaod is uploaded, a new process will be opened, which is different from the original process. To solve this problem, it is necessary to specify session_id, and then judge on the login page that if there is session_id from post, then use the function session_id ($_POST ['PHP_SESSIONID']) to specify 1.
Upload page inside JS, you can get the current SESSION_ID.

Status in ci: 1 Generally, because uploadify and swfupload all use flash clients, the useragent generated by them must be different from the user-agent that users use browsers. Therefore, although the user logs in to your system and generates an session, another session will be generated when the uploader is triggered (with the useragent option turned on above).

So, instead of losing session, CI creates another session for uploadify when you upload the file.

Workaround 1: Set $config ['sess_match_useragent'] to FALSE and try again.

Solution 2 (Recommended): For security reasons, it is not recommended to use Solution 1, but to use another authentication method, such as attaching an token to url and comparing it with token on the server side every time you upload it (for example, this token can be the hash value of the user name). Please refer to the upload verification implementation of stblog for specific implementation method (swfupload is used). --I tried this method, The key point is that if your session class is automatically loaded in autoload, then this is definitely a failure. The solution is to create a new class, for example, MY_Controller inherits from CI_Controller, which is used for background login and needs permission to judge background processing. If upload class inherits from CI_Controller, it will not be verified by session class. Of course, uploading still has to be verified, but it can be verified by post_params

Example:

class Upload extends CI_Controller {
//*******
//*****
}
//~~~~~~~~~~~~~~~~~
class client_photo extends MY_Controller {
    function __construct() {
        parent::__construct();
    }
 function index() {
}
}
//~~~~~~~~~~~~~~~~~~~~~
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
}
}


Related articles: