programmatically uploading an image (via form) and adding to file manager and fileset...

Permalink
Hi there, I'm a bit stuck on this one, although I think I'm on the right track...

I have a form to upload an image which I can get to work using:
<form action="?edit=complete" method="post" enctype="multipart/form-data">
<? if(isset($_FILES['image_upload'])){
      $errors= array();
      $file_name = $_FILES['image_upload']['name'];
      $file_size =$_FILES['image_upload']['size'];
      $file_tmp =$_FILES['image_upload']['tmp_name'];
      $file_type=$_FILES['image_upload']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image_upload']['name'])));
      $expensions= array("jpeg","jpg","png");
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      if($file_size > 2097152){
         $errors[]='File size must be less than 2 MB';
      }
This successfully adds the image (if no errors) to the temp folder /files/tempUploads/ (hurray!)

But what I really need to happen is that on upload, the image is added to the filemanager and ultimately added to a set... I have found this code on the webli.us/cheatsheet which looks like the ticket as it is in the same format as my code above:
// when uploading with a form
  function action_file_upload()   {
    Loader::library("file/importer");
    $fi = new FileImporter();
    $newFile = $fi->import($_FILES['fileName']['tmp_name'],
    $_FILES['fileName']['name']);
    // add file to file set      
    Loader::model('file_set');
    $fs = FileSet::createAndGetSet('FILE_SET_NAME', FileSet::TYPE_PUBLIC, 
    $uID = false);
    $fsf = $fs->addFileToSet($newFile);   
  }
but I cannot seem to combine the 2 to get the desired effect. I know that it is staring me in the face...

Also, is there a simple way to change the image dimensions on upload, to keep under a certain width? just an afterthought....

Thanks in advance for any ideas :)

Rob

rc255
 
rc255 replied on at Permalink Reply
rc255
Any takers? I'm still scratching my head on this one...
rc255 replied on at Permalink Reply
rc255
I've spent the past day investigating this and
function action_file_upload()

only works as a function in a controller?

I am trying to get this to work from a page which I have created in my 'theme'... how exactly does the 'controller' come into it and is this the only way I can get it to work?

Why can't I just include the code in the theme page? If it does only work in a controller, how can I attach a controller to my 'theme page'...?

If anyone can help me out here, I would love to get this working.... I feel that I'm so close, yet so far away...

Thanks

Rob
rc255 replied on at Permalink Best Answer Reply
rc255
Ok, I sorted it with a bit of a hack:

Loader::library("file/importer");
$fi = new FileImporter();
$imgurl='/files/tempUploads/'.$file_name; 
$fi->import($imgurl); // This returns a new FileObject
$newFile = $fi->import($imgurl);

This uploads the file to the temp folder and then adds it to the filemanager

Sorted :)