Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Mysterious Upload Directory


Reply


3 Posts   1082 Views

Avatar
eddieconnecti

Community Member, 26 Posts

16 December 2009 at 3:48am

Got a annoying problem by writing files using onBeforeWrite into an specific custom upload folder. I have searched the forum and found some related topics, but none solved the problem i have. This is my class:

class ArticleItems extends DataObject
{

   static $db = array (
      "Description" => "HTMLText",
      "ItemType" => "Varchar(50)"
   );
   
   static $has_one = array (
      "Page" => "Page",
      "ImageThumbnailFile" => "Image"
   );

   protected static $itemTypes = array(
"IMAGE" => "Image"
   );
   
   public function getCMSFields_forPopup()
   {
      return new FieldSet(
         new DropdownField( "ItemType", "Type of Item", self::$itemTypes ),
         new TextareaField( "Description", "Description" ),
         new ImageField( "ImageThumbnailFile", "Thumbnail" ),
         new FileIFrameField( "DownloadFile", "File for Download" )
      );
   }
   
   
   public function onBeforeWrite()
   {
      
      $file = $this->ImageThumbnailFile;
      $folder = Folder::findOrMake( "Uploads/articles/images" );
      if($file && $folder){
      if($folder->ID != $file->ParentID){
      $file->setParentID($folder->ID);
      $file->write();
      }
      }
      parent::onBeforeWrite();
   }
   
}

If I uplaod the file, it always stores it in the "Uploads/" Directory. My goal is to resize the image and save the thumbnail in "Uploads/articles/images/thumbnails/". The full sized image should be stored in "Uploads/articles/images" and linked to the Item (not set yet). Has anyone an idea of how to do this? Any idea my help...

Avatar
UncleCheese

4085 Posts

16 December 2009 at 4:08am

I don't see any reason why you couldn't just use setUploadFolder("Uploads/articles/images") on your DOM.

Avatar
eddieconnecti

Community Member, 26 Posts

22 December 2009 at 9:39pm

I would like to create three images from the one i upload. This is the reason why i want to set the upload folder for each image. I need a colored thumbnail, a greyscakled thumbnail and an fullsized colored image by maxwidth of 600px. I think it should work with the gd lib. But maybe its better to create an extra class extending the image class with a bunch of methods.

Do you know any examples on writing files, copying files within silverstripe?

And, coming back to your question, where do i set the setUploadFolder?

Thanks!