Welcome Guest, Not a member yet? Register   Sign In
Getting uploadify to work
#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


Messages In This Thread
Getting uploadify to work - by El Forum - 03-25-2009, 08:12 AM
Getting uploadify to work - by El Forum - 03-27-2009, 10:51 PM
Getting uploadify to work - by El Forum - 03-28-2009, 08:16 AM
Getting uploadify to work - by El Forum - 04-04-2009, 05:13 AM
Getting uploadify to work - by El Forum - 04-04-2009, 10:01 AM
Getting uploadify to work - by El Forum - 04-06-2009, 02:45 PM
Getting uploadify to work - by El Forum - 04-10-2009, 09:43 AM
Getting uploadify to work - by El Forum - 04-10-2009, 02:16 PM
Getting uploadify to work - by El Forum - 04-11-2009, 08:14 AM
Getting uploadify to work - by El Forum - 04-11-2009, 09:28 AM
Getting uploadify to work - by El Forum - 04-11-2009, 12:26 PM
Getting uploadify to work - by El Forum - 04-11-2009, 02:41 PM
Getting uploadify to work - by El Forum - 04-15-2009, 08:28 AM
Getting uploadify to work - by El Forum - 05-29-2009, 04:16 PM
Getting uploadify to work - by El Forum - 05-29-2009, 05:12 PM
Getting uploadify to work - by El Forum - 05-29-2009, 07:17 PM
Getting uploadify to work - by El Forum - 05-30-2009, 03:13 AM
Getting uploadify to work - by El Forum - 05-30-2009, 06:08 AM
Getting uploadify to work - by El Forum - 05-31-2009, 08:33 PM
Getting uploadify to work - by El Forum - 06-23-2009, 07:31 PM
Getting uploadify to work - by El Forum - 06-29-2009, 07:46 PM
Getting uploadify to work - by El Forum - 07-08-2009, 01:05 PM
Getting uploadify to work - by El Forum - 07-09-2009, 03:08 AM
Getting uploadify to work - by El Forum - 07-09-2009, 08:04 AM
Getting uploadify to work - by El Forum - 07-09-2009, 09:20 AM
Getting uploadify to work - by El Forum - 07-09-2009, 10:25 AM
Getting uploadify to work - by El Forum - 07-09-2009, 11:35 PM
Getting uploadify to work - by El Forum - 07-10-2009, 05:01 AM
Getting uploadify to work - by El Forum - 07-10-2009, 06:20 AM
Getting uploadify to work - by El Forum - 07-10-2009, 10:19 AM
Getting uploadify to work - by El Forum - 07-12-2009, 08:10 PM
Getting uploadify to work - by El Forum - 07-12-2009, 08:19 PM
Getting uploadify to work - by El Forum - 07-14-2009, 10:17 AM
Getting uploadify to work - by El Forum - 07-23-2009, 01:07 AM
Getting uploadify to work - by El Forum - 07-28-2009, 03:45 AM
Getting uploadify to work - by El Forum - 07-28-2009, 03:50 AM
Getting uploadify to work - by El Forum - 07-28-2009, 02:34 PM
Getting uploadify to work - by El Forum - 08-24-2009, 08:14 AM
Getting uploadify to work - by El Forum - 09-29-2009, 11:39 AM
Getting uploadify to work - by El Forum - 09-29-2009, 10:10 PM
Getting uploadify to work - by El Forum - 09-30-2009, 08:50 AM
Getting uploadify to work - by El Forum - 10-01-2009, 02:51 PM
Getting uploadify to work - by El Forum - 10-08-2009, 11:07 PM
Getting uploadify to work - by El Forum - 10-09-2009, 07:17 AM
Getting uploadify to work - by El Forum - 10-09-2009, 10:24 AM
Getting uploadify to work - by El Forum - 10-14-2009, 12:38 PM
Getting uploadify to work - by El Forum - 10-14-2009, 01:59 PM
Getting uploadify to work - by El Forum - 10-18-2009, 03:32 AM
Getting uploadify to work - by El Forum - 10-26-2009, 01:53 PM
Getting uploadify to work - by El Forum - 10-30-2009, 03:59 AM
Getting uploadify to work - by El Forum - 11-17-2009, 06:40 AM
Getting uploadify to work - by El Forum - 11-17-2009, 06:42 AM
Getting uploadify to work - by El Forum - 11-17-2009, 08:47 AM
Getting uploadify to work - by El Forum - 11-18-2009, 06:18 AM
Getting uploadify to work - by El Forum - 11-18-2009, 10:35 AM
Getting uploadify to work - by El Forum - 11-21-2009, 11:57 AM
Getting uploadify to work - by El Forum - 11-22-2009, 03:20 AM
Getting uploadify to work - by El Forum - 11-23-2009, 08:09 AM
Getting uploadify to work - by El Forum - 11-24-2009, 02:57 AM
Getting uploadify to work - by El Forum - 11-24-2009, 04:04 AM
Getting uploadify to work - by El Forum - 11-24-2009, 04:08 AM
Getting uploadify to work - by El Forum - 11-24-2009, 05:24 AM
Getting uploadify to work - by El Forum - 11-24-2009, 08:10 AM
Getting uploadify to work - by El Forum - 11-24-2009, 02:30 PM
Getting uploadify to work - by El Forum - 11-24-2009, 08:23 PM
Getting uploadify to work - by El Forum - 11-25-2009, 01:29 AM
Getting uploadify to work - by El Forum - 11-25-2009, 03:22 AM
Getting uploadify to work - by El Forum - 11-25-2009, 05:27 AM
Getting uploadify to work - by El Forum - 11-25-2009, 05:42 AM
Getting uploadify to work - by El Forum - 11-25-2009, 07:35 AM
Getting uploadify to work - by El Forum - 11-25-2009, 07:58 AM
Getting uploadify to work - by El Forum - 11-25-2009, 08:41 AM
Getting uploadify to work - by El Forum - 12-01-2009, 01:17 PM
Getting uploadify to work - by El Forum - 12-03-2009, 02:52 AM
Getting uploadify to work - by El Forum - 12-03-2009, 06:13 AM
Getting uploadify to work - by El Forum - 12-03-2009, 07:41 PM
Getting uploadify to work - by El Forum - 12-03-2009, 10:56 PM
Getting uploadify to work - by El Forum - 12-04-2009, 12:55 AM
Getting uploadify to work - by El Forum - 12-04-2009, 08:36 AM
Getting uploadify to work - by El Forum - 12-15-2009, 10:10 PM
Getting uploadify to work - by El Forum - 12-16-2009, 07:41 AM
Getting uploadify to work - by El Forum - 12-31-2009, 02:12 AM
Getting uploadify to work - by El Forum - 12-31-2009, 02:45 PM
Getting uploadify to work - by El Forum - 01-01-2010, 08:26 AM
Getting uploadify to work - by El Forum - 01-02-2010, 12:59 AM
Getting uploadify to work - by El Forum - 01-12-2010, 11:40 PM
Getting uploadify to work - by El Forum - 01-12-2010, 11:44 PM
Getting uploadify to work - by El Forum - 01-12-2010, 11:47 PM
Getting uploadify to work - by El Forum - 01-12-2010, 11:59 PM
Getting uploadify to work - by El Forum - 01-13-2010, 12:19 AM
Getting uploadify to work - by El Forum - 01-17-2010, 12:10 PM
Getting uploadify to work - by El Forum - 01-17-2010, 02:35 PM
Getting uploadify to work - by El Forum - 01-17-2010, 02:55 PM
Getting uploadify to work - by El Forum - 01-17-2010, 04:14 PM
Getting uploadify to work - by El Forum - 01-22-2010, 06:13 AM
Getting uploadify to work - by El Forum - 01-26-2010, 08:41 AM
Getting uploadify to work - by El Forum - 03-06-2010, 05:51 PM
Getting uploadify to work - by El Forum - 03-07-2010, 02:03 AM
Getting uploadify to work - by El Forum - 03-08-2010, 07:57 AM
Getting uploadify to work - by El Forum - 03-13-2010, 07:00 PM
Getting uploadify to work - by El Forum - 03-13-2010, 11:57 PM
Getting uploadify to work - by El Forum - 06-08-2010, 02:54 AM
Getting uploadify to work - by El Forum - 06-08-2010, 07:01 AM
Getting uploadify to work - by El Forum - 06-14-2010, 03:54 AM
Getting uploadify to work - by El Forum - 06-14-2010, 06:51 AM
Getting uploadify to work - by El Forum - 06-14-2010, 07:27 AM
Getting uploadify to work - by El Forum - 06-14-2010, 01:01 PM
Getting uploadify to work - by El Forum - 06-14-2010, 03:08 PM
Getting uploadify to work - by El Forum - 06-15-2010, 03:39 AM
Getting uploadify to work - by El Forum - 06-15-2010, 05:47 AM
Getting uploadify to work - by El Forum - 07-08-2010, 01:41 PM
Getting uploadify to work - by El Forum - 08-07-2010, 12:37 AM
Getting uploadify to work - by El Forum - 11-10-2010, 10:26 AM
Getting uploadify to work - by El Forum - 11-10-2010, 10:31 AM
Getting uploadify to work - by El Forum - 11-10-2010, 10:34 AM
Getting uploadify to work - by El Forum - 11-10-2010, 10:52 AM
Getting uploadify to work - by El Forum - 11-10-2010, 10:59 AM
Getting uploadify to work - by El Forum - 11-11-2010, 03:00 AM
Getting uploadify to work - by El Forum - 11-11-2010, 06:04 AM
Getting uploadify to work - by El Forum - 01-28-2011, 04:11 AM
Getting uploadify to work - by El Forum - 03-25-2011, 06:16 AM
Getting uploadify to work - by El Forum - 03-28-2011, 08:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB