Home >Backend Development >PHP Tutorial >The best solution to integrate codeigniter with swfupload_PHP tutorial
codeigniter is a lightweight and convenient MVC framework. The recent project involves batch uploading, so I used the swfupload plug-in. Although there are many posts about ci and swfupload on the Internet, they are not very complete. Therefore, here is a summary based on the advantages of each company and based on my own practical experience.
Question 1: Can’t get the file type?
Answer: Since the MIME of all types of files in SWFUPLOAD is application/octet-stream, using the upload class that comes with Codeigniter cannot accurately determine whether it is an image. The answer has been given according to the official ci wiki
In your view file, include the SWFUpload javascript file:
Answer:
General method: When swfuplaod uploads, it will open a new process, which is inconsistent with the original process. To solve this problem, you need to specify the session_id, and then judge on the login page. If there is a posted session_id, then use the function session_id ($_POST['PHP_SESSIONID']) Specify it.
In the JS of the upload page, you can get the current SESSION_ID.
Status in CI: Under normal circumstances, because uploadify and swfupload use flash clients, the useragent they generate must be different from the user-agent used by the user in the browser. Therefore, although a session is generated when the user logs into your system, another session will be generated when the upload process is triggered (when the useragent option mentioned above is turned on).
So, it is not that the session is lost, but when you upload the file, CI creates another session for uploadify.
Solution 1: Set $config['sess_match_useragent'] to FALSE and try again.
Solution 2 (Recommended): For security reasons, it is not recommended to use the first solution. Instead, use another verification method, such as appending a token to the URL each time you upload it. Compare with the server-side token (for example, this token can be the hash value of the user name). For specific implementation methods, please refer to stblog's upload verification implementation (using swfupload). ----I tried this method. The key point is that if your session class is automatically loaded in autoload, then this will definitely fail. The solution is to create a new class, for example, MY_Controller inherits from CI_Controller, using To log in to the background, you need permission to determine the background processing. If the upload class inherits from CI_Controller, it will not be verified by the session class. Of course, the upload still needs to be verified, but you can use post_params to pass the value for verification:
Example: