Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Before going on, I suggest you drop any flash uploader, and pick up <a href="https://github.com/blueimp/jQuery-File-Upload" rel="nofollow">https://github.com/blueimp/jQuery-File-Upload</a> that stays always HTML5 and is backwards compatible. At most you won't see uploading bars, but there's no Flash, and you don't have to do any kind of modification to CodeIgniter.</p> <p>The following is not very refined. In fact, I'm dropping support for Flash upload in my application from the next version.</p> <p>Any upload made via Flash will be received by the server as application/octet-stream. This stops being a problem when in the file /application/config/mime.php you add "application/octet-stream" to the filetypes you're interested in. Here's <a href="https://github.com/woxxy/FoOlSlide/blob/master/application/config/mimes.php" rel="nofollow">an example</a>, look at the bottom of the file.</p> <p>Currently, the CSRF is not much the problem, as much that Flash has its own cookies, which means it's like a completely separate browser. Before even trying to send the CSRF, we must put into Flash the Session ID that CodeIgniter uses to identify you. You will also have to change in /application/config/config.php</p> <pre><code>$config['sess_match_useragent'] = FALSE; </code></pre> <p>If you want to use Uploadify3 (this should work for Uploadify 2 and Plupload too), you first have to add /application/libraries/MY_Session.php to make possible to send Session data also via POST.</p> <p>Just use this file: <a href="https://github.com/woxxy/FoOlSlide/blob/a7522d747fe406da18ce18ae9763f083b89eb91e/application/libraries/MY_Session.php" rel="nofollow">https://github.com/woxxy/FoOlSlide/blob/a7522d747fe406da18ce18ae9763f083b89eb91e/application/libraries/MY_Session.php</a></p> <p>Then in your controller, you have to make it possible to retrieve the Session ID at any time.</p> <pre><code>function get_sess_id() { $this-&gt;output-&gt;set_output(json_encode(array('session' =&gt; $this-&gt;session-&gt;get_js_session(), 'csrf' =&gt; $this-&gt;security-&gt;get_csrf_hash()))); } </code></pre> <p>Your upload controller should be a pretty standard upload function. Make sure you use the right name on your upload ("userfile").</p> <p>Now, the worst part: the view file. I could've removed some details, but I think some extra data will help you coding it without having to look up too much in Uploadify3.</p> <pre><code>&lt;script type="text/javascript"&gt; function updateSession() { jQuery.post('&lt;?php echo site_url('/admin/series/get_sess_id'); ?&gt;', function(result){ jQuery('#file_upload_flash').uploadifySettings( 'postData', { 'ci_sessionz' : result.session, '&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;' : result.csrf, 'chapter_id' : &lt;?php echo $chapter-&gt;id; ?&gt; }, false ); setTimeout('updateSession()', 6000); }, 'json'); } jQuery(document).ready(function() { jQuery('#file_upload_flash').uploadify({ 'swf' : '&lt;?php echo site_url(); ?&gt;assets/uploadify/uploadify.swf', 'uploader' : '&lt;?php echo site_url('/admin/series/upload/compressed_chapter'); ?&gt;', 'cancelImage' : '&lt;?php echo site_url(); ?&gt;assets/uploadify/uploadify-cancel.png', 'checkExisting' : false, 'preventCaching' : false, 'multi' : true, 'buttonText' : '&lt;?php echo _('Use flash upload'); ?&gt;', 'width': 200, 'auto' : true, 'requeueErrors' : true, 'uploaderType' : 'flash', 'postData' : {}, 'onSWFReady' : function() { updateSession(); }, 'onUploadSuccess' : function(file, data, response) { var files = jQuery.parseJSON(data); var fu = jQuery('#fileupload').data('fileupload'); fu._adjustMaxNumberOfFiles(-files.length); fu._renderDownload(files) .appendTo(jQuery('#fileupload .files')) .fadeIn(function () { jQuery(this).show(); }); } }); }); &lt;/script&gt; &lt;div id="file_upload_flash"&gt;&lt;/div&gt; </code></pre> <p>Now, if it wasn't already enough work... there's a bug in Uploadify3, that doesn't make it trigger one or two of the callbacks.</p> <p>Here's a fixed version of the code: <a href="https://github.com/woxxy/FoOlSlide/blob/a7522d747fe406da18ce18ae9763f083b89eb91e/assets/uploadify/jquery.uploadify.js" rel="nofollow">https://github.com/woxxy/FoOlSlide/blob/a7522d747fe406da18ce18ae9763f083b89eb91e/assets/uploadify/jquery.uploadify.js</a></p> <p>You might want to minify it.</p> <p><strong>But what if you wanted to use jQuery-File-Upload?</strong></p> <p>Then all you'd have to do is adapting your controller a bit. Here's an example (I won't go through cleaning this code either, as it would probably just make a broken upload controller anyway)</p> <pre><code>function upload() { $info = array(); // compatibility for flash uploader and browser not supporting multiple upload if (is_array($_FILES['Filedata']) &amp;&amp; !is_array($_FILES['Filedata']['tmp_name'])) { $_FILES['Filedata']['tmp_name'] = array($_FILES['Filedata']['tmp_name']); $_FILES['Filedata']['name'] = array($_FILES['Filedata']['name']); } for ($file = 0; $file &lt; count($_FILES['Filedata']['tmp_name']); $file++) { $valid = explode('|', 'png|zip|rar|gif|jpg|jpeg'); if (!in_array(strtolower(substr($_FILES['Filedata']['name'][$file], -3)), $valid)) continue; if (!in_array(strtolower(substr($_FILES['Filedata']['name'][$file], -3)), array('zip', 'rar'))) $pages = $this-&gt;files_model-&gt;page($_FILES['Filedata']['tmp_name'][$file], $_FILES['Filedata']['name'][$file], $this-&gt;input-&gt;post('chapter_id')); else $pages = $this-&gt;files_model-&gt;compressed_chapter($_FILES['Filedata']['tmp_name'][$file], $_FILES['Filedata']['name'][$file], $this-&gt;input-&gt;post('chapter_id')); foreach ($pages as $page) { $info[] = array( 'name' =&gt; $page-&gt;filename, 'size' =&gt; $page-&gt;size, 'url' =&gt; $page-&gt;page_url(), 'thumbnail_url' =&gt; $page-&gt;page_url(TRUE), 'delete_url' =&gt; site_url("admin/series/delete/page"), 'delete_data' =&gt; $page-&gt;id, 'delete_type' =&gt; 'POST' ); } } // return a json array echo json_encode($info); return true; } function get_file_objects() { // Generate JSON File Output (Required by jQuery File Upload) header('Content-type: application/json'); header('Pragma: no-cache'); header('Cache-Control: private, no-cache'); header('Content-Disposition: inline; filename="files.json"'); $id = $this-&gt;input-&gt;post('id'); $chapter = new Chapter($id); $pages = $chapter-&gt;get_pages(); $info = array(); foreach ($pages as $page) { $info[] = array( 'name' =&gt; $page['filename'], 'size' =&gt; intval($page['size']), 'url' =&gt; $page['url'], 'thumbnail_url' =&gt; $page['thumb_url'], 'delete_url' =&gt; site_url("admin/series/delete/page"), 'delete_data' =&gt; $page['id'], 'delete_type' =&gt; 'POST' ); } echo json_encode($info); return true; } </code></pre> <p>And add more tremendous view code (this time it's almost stock one from jQuery upload)</p> <pre><code>&lt;div id="fileupload"&gt; &lt;link href="&lt;?php echo site_url(); ?&gt;assets/jquery-file-upload/jquery-ui.css" rel="stylesheet" id="theme" /&gt; &lt;link href="&lt;?php echo site_url(); ?&gt;assets/jquery-file-upload/jquery.fileupload-ui.css" rel="stylesheet" /&gt; &lt;?php echo form_open_multipart(""); ?&gt; &lt;div class="fileupload-buttonbar"&gt; &lt;label class="fileinput-button"&gt; &lt;span&gt;Add files...&lt;/span&gt; &lt;input type="file" name="Filedata[]" multiple&gt; &lt;/label&gt; &lt;button type="submit" class="start"&gt;Start upload&lt;/button&gt; &lt;button type="reset" class="cancel"&gt;Cancel upload&lt;/button&gt; &lt;button type="button" class="delete"&gt;Delete files&lt;/button&gt; &lt;/div&gt; &lt;?php echo form_close(); ?&gt; &lt;div class="fileupload-content"&gt; &lt;table class="files"&gt;&lt;/table&gt; &lt;div class="fileupload-progressbar"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;script id="template-upload" type="text/x-jquery-tmpl"&gt; &lt;tr class="template-upload{{if error}} ui-state-error{{/if}}"&gt; &lt;td class="preview"&gt;&lt;/td&gt; &lt;td class="name"&gt;${name}&lt;/td&gt; &lt;td class="size"&gt;${sizef}&lt;/td&gt; {{if error}} &lt;td class="error" colspan="2"&gt;Error: {{if error === 'maxFileSize'}}File is too big {{else error === 'minFileSize'}}File is too small {{else error === 'acceptFileTypes'}}Filetype not allowed {{else error === 'maxNumberOfFiles'}}Max number of files exceeded {{else}}${error} {{/if}} &lt;/td&gt; {{else}} &lt;td class="progress"&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt; &lt;td class="start"&gt;&lt;button&gt;Start&lt;/button&gt;&lt;/td&gt; {{/if}} &lt;td class="cancel"&gt;&lt;button&gt;Cancel&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; &lt;script id="template-download" type="text/x-jquery-tmpl"&gt; &lt;tr class="template-download{{if error}} ui-state-error{{/if}}"&gt; {{if error}} &lt;td&gt;&lt;/td&gt; &lt;td class="name"&gt;${name}&lt;/td&gt; &lt;td class="size"&gt;${sizef}&lt;/td&gt; &lt;td class="error" colspan="2"&gt;Error: {{if error === 1}}File exceeds upload_max_filesize (php.ini directive) {{else error === 2}}File exceeds MAX_FILE_SIZE (HTML form directive) {{else error === 3}}File was only partially uploaded {{else error === 4}}No File was uploaded {{else error === 5}}Missing a temporary folder {{else error === 6}}Failed to write file to disk {{else error === 7}}File upload stopped by extension {{else error === 'maxFileSize'}}File is too big {{else error === 'minFileSize'}}File is too small {{else error === 'acceptFileTypes'}}Filetype not allowed {{else error === 'maxNumberOfFiles'}}Max number of files exceeded {{else error === 'uploadedBytes'}}Uploaded bytes exceed file size {{else error === 'emptyResult'}}Empty file upload result {{else}}${error} {{/if}} &lt;/td&gt; {{else}} &lt;td class="preview"&gt; {{if thumbnail_url}} &lt;a href="${url}" target="_blank"&gt;&lt;img src="${thumbnail_url}"&gt;&lt;/a&gt; {{/if}} &lt;/td&gt; &lt;td class="name"&gt; &lt;a href="${url}"{{if thumbnail_url}} target="_blank"{{/if}}&gt;${name}&lt;/a&gt; &lt;/td&gt; &lt;td class="size"&gt;${sizef}&lt;/td&gt; &lt;td colspan="2"&gt;&lt;/td&gt; {{/if}} &lt;td class="delete"&gt; &lt;button data-type="${delete_type}" data-url="${delete_url}" data-id="${delete_data}"&gt;Delete&lt;/button&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; &lt;script src="&lt;?php echo site_url(); ?&gt;assets/js/jquery-ui.js"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo site_url(); ?&gt;assets/js/jquery.tmpl.js"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo site_url(); ?&gt;assets/jquery-file-upload/jquery.fileupload.js"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo site_url(); ?&gt;assets/jquery-file-upload/jquery.fileupload-ui.js"&gt;&lt;/script&gt; &lt;script src="&lt;?php echo site_url(); ?&gt;assets/jquery-file-upload/jquery.iframe-transport.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; jQuery(function () { jQuery('#fileupload').fileupload({ url: '&lt;?php echo site_url('/admin/series/upload/compressed_chapter'); ?&gt;', sequentialUploads: true, formData: [ { name: 'chapter_id', value: &lt;?php echo $chapter-&gt;id; ?&gt; } ] }); jQuery.post('&lt;?php echo site_url('/admin/series/get_file_objects'); ?&gt;', { id : &lt;?php echo $chapter-&gt;id; ?&gt; }, function (files) { var fu = jQuery('#fileupload').data('fileupload'); fu._adjustMaxNumberOfFiles(-files.length); fu._renderDownload(files) .appendTo(jQuery('#fileupload .files')) .fadeIn(function () { jQuery(this).show(); }); }); jQuery('#fileupload .files a:not([target^=_blank])').live('click', function (e) { e.preventDefault(); jQuery('&lt;iframe style="display:none;"&gt;&lt;/iframe&gt;') .prop('src', this.href) .appendTo('body'); }); }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload