Welcome Guest, Not a member yet? Register   Sign In
Getting uploadify to work
#11

[eluser]Bramme[/eluser]
Hmm after thought: too bad this doesn't work with CodeIgniter URL's (I should look into that) because if it would work, I could resize them upon upload, instead of afterwards.
#12

[eluser]Johan André[/eluser]
[quote author="Bramme" date="1239492391"]Hmm after thought: too bad this doesn't work with CodeIgniter URL's (I should look into that) because if it would work, I could resize them upon upload, instead of afterwards.[/quote]

Yep, thats my problem too...
If you find a way to deal with it, please let me know... Smile
#13

[eluser]ubl_coder[/eluser]
This plugin is cool i think. I tried the way M_K do and it worked !

How to store uploaded file name like "images1.jpg" into a field in database?

In my project that file name should be stored in database. Thanks for solutions.
#14

[eluser]Bob Sawyer[/eluser]
Bumping this thread to find out if anyone has solved this. Meanwhile, I'm banging my head against the keyboard between attempts. Please hurry -- my keyboard won't last much longer. :-P
#15

[eluser]Bramme[/eluser]
Haven't really looked. Just used the included php file and then processed all images with the form...
#16

[eluser]Bob Sawyer[/eluser]
Right, which is great if all you need to do is upload. My script has to record a number of other items (user ID, file metadata, timestamp, etc.) into the database, and fire off a couple other functions as well. Thus keeping it in the current controller has its benefits! :-)
#17

[eluser]Bramme[/eluser]
True, what you could do at the moment is make use of the "oncomplete" (or whatever it's called) callback and add some javascript that adds hidden fields to your form with the data you need.

If your users need to enter some more data (like they had to in my application), you can use a submit button for them, if they don't and they just need to upload the files, you can use the onallcomplete event to submit the form...
#18

[eluser]pixelsandcode[/eluser]
Yep, that's what I started working on last night. Slowly but surely we'll beat this! ;-)
#19

[eluser]Bob Sawyer[/eluser]
OK, got it working. I thought I'd share my code in hopes that someone benefits from it.

As we've previously discovered, Uploadify does not work with CI's MVC structure or its "pretty URLs," so following the lead of others in this thread, I used the upload.php script included in the Uploadify archive. But I needed some additional functionality -- additional values needed to be passed to the script, and the file data needed to be inserted into the database after upload.

First, I copied and modified several functions from CI's Upload lib that I needed to prep the files:

Code:
function set_filename($path, $filename, $file_ext) {
   mt_srand();
   $filename = md5(uniqid(mt_rand())).$file_ext;    
   if ( ! file_exists($path.$filename)) {
      return $filename;
   } else {
      $filename = str_replace($file_ext, '', $filename);
      $new_filename = '';
      for ($i = 1; $i < 100; $i++) {            
         if ( ! file_exists($path.$filename.$i.$file_ext)) {
            $new_filename = $filename.$i.$file_ext;
            break;
         }
      }
      $new_filename .= '.'.$file_ext;
      return $new_filename;
   }
}

function prep_filename($filename) {
   if (strpos($filename, '.') === FALSE) {
      return $filename;
   }
   $parts = explode('.', $filename);
   $ext = array_pop($parts);
   $filename    = array_shift($parts);
   foreach ($parts as $part) {
      $filename .= '.'.$part;
   }
   $filename .= '.'.$ext;
   return $filename;
}

function get_extension($filename) {
   $x = explode('.', $filename);
   return '.'.end($x);
}

Then I added a few additional checks, tweaks, and vars to the existing "if" statement:

Code:
if (!empty($_FILES)) {
   $path = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
   $client_id = $_GET['client_id'];
   $file_temp = $_FILES['userfile']['tmp_name'];
   $file_name = prep_filename($_FILES['userfile']['name']);
   $file_ext = get_extension($_FILES['userfile']['name']);
   $real_name = $file_name;
   $newf_name = set_filename($path, $file_name, $file_ext);
   $file_size = round($_FILES['userfile']['size']/1024, 2);
   $file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['userfile']['type']);
   $file_type = strtolower($file_type);
   $file_ext = get_extension($_FILES['userfile']['name']);
   $targetFile =  str_replace('//','/',$path) . $newf_name;
   move_uploaded_file($file_temp,$targetFile);

   $filearray = array();
   $filearray['file_name'] = $newf_name;
   $filearray['real_name'] = $real_name;
   $filearray['file_ext'] = $file_ext;
   $filearray['file_size'] = $file_size;
   $filearray['file_path'] = $targetFile;
   $filearray['client_id'] = $client_id;

   $json_array = json_encode($filearray);
   echo $json_array;
}

Note the existence of "$_GET['client_id']" and the json_encoded array. That's going to help me post this info to the db. Now, my Javascript...

The dashboard controller's index() function checks the user role and applies a number of different variables through a switch() statement. We're assuming that a user with the role of "Client" has logged in and wants to upload files:

Code:
switch($role_name) {
      case "Client":
      $client_id = $this->Sql->getClientUserClientId($user_id);
      $uploadify = <<<JS
&lt;script type="text/javascript"&gt;
$(document).ready(function() { // wait for document to load  
   $('#userfile').fileUpload({
      'uploader' : '/js/uploader.swf',
      'script' : '/uploadify/upload.php',
      'scriptData' : {'client_id':'
JS;
      
      $uploadify.= $client_id;
      $uploadify.= <<<JS2
'},
      'scriptAccess' : 'always',
      'multi' : true,
      'fileDataName' : 'userfile',
      'cancelImg' : '/images/icon-delete.gif',
      'auto' : true,
      'folder' : '/files',
      'fileExt' : '*.doc;*.docx;*.pdf;*.jpg;*.png;*.zip',
      'fileDesc' : 'Select files of type .doc, .pdf, .jpg, .png, or .zip',
      'sizeLimit' : '3072000',
      'simUploadLimit' : '8',
      'onError' : function (a, b, c, d) {
         if (d.status == 404)
            alert('Could not find upload script. Use a path relative to: '+'&lt;?= getcwd() ?&gt;');
         else if (d.type === "HTTP")
            alert('error '+d.type+": "+d.status);
         else if (d.type ==="File Size")
            alert(c.name+' '+d.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB');
         else
            alert('error '+d.type+": "+d.text);
      },
      'onComplete' : function (event, queueID, fileObj, response, data) {
         $.post("/admin/dashboard/uploadify/", { 'filearray' : response });
      },
      'onAllComplete': function(event, data) {
         alert("Uploading Complete!\\nTotal uploaded: "+data.filesUploaded+"\\nTotal errors: "+data.errors+"\\nClose this box to refresh the page and see your uploaded files.");
         [removed].href="/admin/dashboard";
      }
   });
...

Note that I'm using the onComplete callback of Uploadify to post the 'response' value to my dashboard controller's uploadify() function, which looks like this:

Code:
function uploadify() {
     $this->Sql->insertFile(json_decode($_POST['filearray']));
}

Simple DB insert, nothing else. I use Uploadify's onAllComplete callback to pop an alert on the user to let them know that all files have been uploaded and other info. When they click “OK” to close the alert box, the page refreshes and they can see their uploaded files displayed in a list with other uploaded files.

I hope this helps someone. It took me most of the weekend, muddling through trial and error and looking up possible solutions on this board, the Uploadify boards, Google Groups, and a few others before I finally stumbled across the final solution earlier this evening.

Cheers,
Bob
#20

[eluser]Unknown[/eluser]
Hi there,

This is an interesting thread. I've also implemented uploadify as a multiple file uploader... problem is, if my users select too many files, the upload queue gets too big, and some file uploads fail... has anyone made any code that ensures the upload queue is kept below a certain limit? I am experimenting with the onSelect event now...

please let me know! Thanks~




Theme © iAndrew 2016 - Forum software by © MyBB