Publish picture

I still cannot publish any image???

I have a test image in uploads folder like this:

BE

-> uploads -> img.jpg

I have a ImageAsset file in assets folder:

BE

-> assets -> ImageAsset.php

it looks like this:


 namespace backend\assets;


  use yii\web\AssetBundle;

  use Yii;


  class ImageAsset extends AssetBundle

  {

     public $sourcePath = '@backend/uploads'; 

  }

…and view file looks like this:


use backend\assets\ImageAsset;


ImageAsset::register($this);

How can I publish that test image?

I have read dozen of articles and cant’t find the answer.

what do you mean by publish an Image? Why do you want to use an asset bundle for an image? Your image appears to be in a web accessible directory and you shouldn’t have to publish it.

If you want to display the image in your browser you could do something like.


<img src="<?= Yii::$app->request->baseUrl.'/uploads/img.jpg'?>">

…so this defines that folder is web accessible:


 namespace backend\assets;


  use yii\web\AssetBundle;

  use Yii;


  class ImageAsset extends AssetBundle

  {

     public $sourcePath = '@backend/uploads'; 

  }

…am I right?

This does not give any errors,


<img src="<?= Yii::$app->request->baseUrl.'/uploads/img.jpg'?>">

but it does not display the image.

It shows as a broken link.

Link in firefox’s source code looks like this:


<img src="/yiiadvanced/backend/uploads/img.jpg">

No, asset bundle will publish the assets listed (i.e. $js=[ js/scripts]) into the folder you choose. It doesn’t necessarily mean it’s web accessible. So in your case it will publish the files into @backend/uploads (@backend would be an alias that is set and it might not really be the “backend” if you messed with its path) ; weather @backed/uploads is web accessible or not is will depend on your server settings. With that said it’s up to you to make sure it’s web accessible and writable.

so an asset bundle in

yourApp > backend > assetundles >yourAssetBundle.php

by default would publish the asset to your backend web root assets folder

i.e.

yourApp > backend > web > assets > yourAssets

if your setup like default advanced app it should be

yourApp > backend > web > uploads > img.jpg

Did you change your basePath or baseUrl in your backend > config > main.php / backend > config > main-local.php files?

Did you change your folder structure?

What is your folder structure starting at the web root and ending at the image?

AssetBundle is for static assets rather than user-uploaded content. I usually use a controller action to publish uploaded images, when there’s a reason not to upload them directly to a web-accessible directory. On the fist request the new image cannot be found in the public directory and the URL rewriting mechanism initializes the web application that routes the request to an action. The action sends the image to the client and copies it to the public directory, so subsequent requests can find it there.

I am using nenad-zivkovic/yii2-advanced-template.

Folder structure is myApp > backend > uploads > img.jpg

There is no web folder this structure.

Is it possible to upload files/images to web_root i.e. public_html?

Outside the _protected folder?

to here: MyApp > uploads/images/

Not here: MyApp > _protected > backend > uploads/

My mistake.

My question wasn’t clear enough.

These works fine.


<img src="<?= Yii::$app->request->baseUrl . '/uploads/img.jpg' ?>" > 


<?= Html::img('@web/uploads/img.jpg') ?>

…when image is in webroot.

Path: MyApp > backend > uploads > img.jpg

…but I was trying to get image from here:

Path: MyApp > _protected > backend > uploads >img.jpg