Link to home
Start Free TrialLog in
Avatar of wouta
wouta

asked on

Is there any solution for getting the full file paths in the backend?

I try in Ocotbercms to get the full image url in the backend.

In need in the backend the full file path of a uploaded image so I can used the image url in afterCreate().
My model:

<?php 
namespace name\pluginname\Models;

use Model;
use DB;
use Validator; 

/**
 * Post Model
 */
class Post extends Model
{

    public $table = mytable';
    protected $guarded = ['*'];
    public $attachOne = [
    'featured_images' => ['System\Models\File']
    ];

    public function afterCreate()
    {
    $post = Post::find($this->id); 

    if ($post->featured_images)
    $featuredImage = $post->featured_images->ge<wbr ></wbr><wbr ></wbr>tPath();
    else
    $featuredImage = 'http://placehold.it/220x3<wbr ></wbr><wbr ></wbr>00';

    traceLog('image url: '. $featuredImage);

    }

}

Open in new window


The var $featuredImage has always the placeholder image because $post->featured_images is empty. I do something wrong but I realy don't see what I do wrong...
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

I know very little (to nothing) of PHP but your solution should be in the $_SERVER.
Inspect it and you should find what you need.
I have never heard of Ocotbercms, but the code looks like PHP.  You can find the complete URL of the existing script with something like this.
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Open in new window

The path to the current working directory is available in getcwd().

To learn a bit more about what $post contains, you might want to do something like this:
/* Other Code */
$post = Post::find($this->id);
ob_start();
var_dump($post);
$msg = ob_get_clean();
tracelog('post: ' . $msg);

Open in new window

Avatar of wouta
wouta

ASKER

I have done that before. When I dump $post:

[2015-07-08 10:37:42] production.INFO: post: object(Addrenaline\Blog\Models\Post)#640 (40) {
  ["table"]=>
  string(22) "addrenaline_blog_posts"
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["hasOne"]=>
  array(0) {
  }
  ["hasMany"]=>
  array(0) {
  }
  ["belongsTo"]=>
  array(0) {
  }
  ["belongsToMany"]=>
  array(1) {
    ["tags"]=>
    array(2) {
      [0]=>
      string(28) "Addrenaline\Blog\Models\Tags"
      ["table"]=>
      string(27) "addrenaline_blog_posts_tags"
    }
  }
  ["morphTo"]=>
  array(0) {
  }
  ["morphOne"]=>
  array(0) {
  }
  ["morphMany"]=>
  array(0) {
  }
  ["attachOne"]=>
  array(1) {
    ["featured_images"]=>
    array(1) {
      [0]=>
      string(18) "System\Models\File"
    }
  }
  ["attachMany"]=>
  array(0) {
  }
  ["implement"]=>
  NULL
  ["attributes"]=>
  array(18) {
    ["id"]=>
    int(171)
    ["titel"]=>
    string(4) "adad"
    ["slug"]=>
    string(4) "adad"
    ["content"]=>
    string(18) "<p>​asdasdas</p>"
    ["published"]=>
    string(0) ""
    ["tags"]=>
    string(0) ""
    ["published_at"]=>
    string(19) "2015-07-01 11:00:00"
    ["seoTitel"]=>
    string(4) "adad"
    ["MetaRobotsIndex"]=>
    string(0) ""
    ["excerpt"]=>
    string(0) ""
    ["MetaRobotsFollow"]=>
    string(0) ""
    ["metaOmschrijving"]=>
    string(0) ""
    ["sitemap"]=>
    int(1)
    ["metarobots"]=>
    string(4) "none"
    ["SitemapPrioriteit"]=>
    string(0) ""
    ["created_at"]=>
    string(19) "2015-07-08 10:37:42"
    ["updated_at"]=>
    string(19) "2015-07-08 10:37:42"
    ["featured_images"]=>
    string(0) ""
  }
  ["jsonable":protected]=>
  array(0) {
  }
  ["dates":protected]=>
  array(0) {
  }
  ["morphToMany"]=>
  array(0) {
  }
  ["morphedByMany"]=>
  array(0) {
  }
  ["hasManyThrough"]=>
  array(0) {
  }
  ["connection":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["timestamps"]=>
  bool(true)
  ["original":protected]=>
  array(18) {
    ["id"]=>
    int(171)
    ["titel"]=>
    string(4) "adad"
    ["slug"]=>
    string(4) "adad"
    ["content"]=>
    string(18) "<p>​asdasdas</p>"
    ["published"]=>
    string(0) ""
    ["tags"]=>
    string(0) ""
    ["published_at"]=>
    string(19) "2015-07-01 11:00:00"
    ["seoTitel"]=>
    string(4) "adad"
    ["MetaRobotsIndex"]=>
    string(0) ""
    ["excerpt"]=>
    string(0) ""
    ["MetaRobotsFollow"]=>
    string(0) ""
    ["metaOmschrijving"]=>
    string(0) ""
    ["sitemap"]=>
    int(1)
    ["metarobots"]=>
    string(4) "none"
    ["SitemapPrioriteit"]=>
    string(0) ""
    ["created_at"]=>
    string(19) "2015-07-08 10:37:42"
    ["updated_at"]=>
    string(19) "2015-07-08 10:37:42"
    ["featured_images"]=>
    string(0) ""
  }
  ["relations":protected]=>
  array(0) {
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["appends":protected]=>
  array(0) {
  }
  ["casts":protected]=>
  array(0) {
  }
  ["touches":protected]=>
  array(0) {
  }
  ["observables":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["morphClass":protected]=>
  NULL
  ["exists"]=>
  bool(true)
  ["emitterSingleEventCollection":protected]=>
  array(0) {
  }
  ["emitterEventCollection":protected]=>
  array(0) {
  }
  ["emitterEventSorted":protected]=>
  array(0) {
  }
  ["extensionData":protected]=>
  array(3) {
    ["extensions"]=>
    array(0) {
    }
    ["methods"]=>
    array(0) {
    }
    ["dynamicMethods"]=>
    array(0) {
    }
  }
  ["sessionKey"]=>
  NULL
}

Open in new window


When I dump $this

production.INFO: THIS: {"titel":"adad","slug":"adad","content":"<p>\u200basdasdas<\/p>","published_at":"2015-07-01 11:00:00","seoTitel":"adad","sitemap":"1","metarobots":"none","updated_at":"2015-07-08 10:41:51","created_at":"2015-07-08 10:41:51","id":"172","featured_images":null}  

Open in new window


In both dump are nothing about the uploaded image....
Please post a link to the documentation that describes how to upload images, thanks.
Avatar of wouta

ASKER

First maybe good to know Octobercms is build on top of Laravel.
The upload function use a widget form and the form field are set in a yaml file, https://octobercms.com/docs/backend/forms#widget-fileupload. The upload part of the YAML file like:
featured_images:
            tab: Publiceren
            label: Uitgelichte afbeelding
            type: fileupload
            mode: image
            imageWidth: 948
            imageHeight: 248

Open in new window


The part about attachments you can find here: https://octobercms.com/docs/database/model#file-attachments

Deferred binding is the relationship between the image and the post and will generated automaticly in the backend (where I add the image is the backend). https://octobercms.com/docs/database/model#deferred-binding

So far I know is this all documentation to upload a image.

To be more clear what happens. The image wil be uploaded and the orginal en new filename wil be saved in de system_files table. Only in that table is not set the whole path to the file.

When I run the follow code:
example code form: https://octobercms.com/docs/database/model#attachments-usage-example
// Find the Blog Post model again
$post = Post::find($this->id); 

// Look for the featured image address, otherwise use a default one
if ($post->featured_images)
    $featuredImage = $post->featured_images->ge<wbr ></wbr>t->Path();<wbr ></wbr>
else
    $featuredImage = 'http://placehold.it/220x3<wbr ></wbr><wbr ></wbr>00';

<img src="<?= $featuredImage ?>" alt="Featured Image">

Open in new window


I get always the placeholder image. This is because
$post->featured_images

Open in new window

is empty. In the database of the blog is the field featured_images also empty so the system don't write anything to that field.

I hope is have explain things clear....
Yes, I thought I recognized some of the Eloquent ORM in that code!  Let me look at it a bit more.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wouta

ASKER

Ye I was just reading it... Now I know how things build up I geuss I can create the url by my own....
Yeah, it would be worth a try.  The three-tier fragment name is an "interesting" directory design.
Avatar of wouta

ASKER

So far I see is this the most easy way to do this right now. I did not know that they create this way the directory structure....