About Images Directory Separator

Hi All,

In config file of my app I have item for images directory like :


  'images_dir' => 'assets/mine/images/',



I use this config item in both cases when I need to generate relative url for some images and when I have to generate path to directory, when I upload image to disk.

It worked ok on linux, but when site was run on Windows it gave errors, as on windows different directory separator used and uploading images to disk error is raised as path is invalid.

It looks like that the desision can be to make 2 config items like :


  'images_dir' =>  'assets' . DIRECTORY_SEPARATOR . 'mine' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR, //; Use it when uploading images to directory

  'images_dir_url' => 'assets/mine/images/',  // use it for relative urls for images



I dislike this way, but which decision is the best ? If yii 1.1.15 has some tools for this ?

what I do personally, add a helper image() function it works cross platform




<?php

// customer helper

function image($name, $title='', $opts=array())

{

    $file = Yii::app()->request->baseUrl."/assets/".$file;

    return CHtml::image($file, $title, $opts);

}


?>


// in views i use it like so

<?= image('css/imagename.png') ?>




Even better, wrap it up in a separate helper class and put it in your components directory. It’ll be easier to add new functionality and methods that way.

The assets directory is the wrong place to put your images. Yii treats it as a temporary directory for assets published by application components. You should be able to empty the assets directory without any negative impact on your application.

I’m assuming above that you’re using the assets directory in the web root. If that’s not the case and you’re actually publishing them from your own components, feel free to ignore me.

So use CHtml::image( for showing images as it is cross platform.

But when I work with directory :

  1. uploading files I use move_uploaded_file functions and paths for it must be in valid OS format.

If yii has cross platform substitution for it ?

  1. The similar question as for reading files of directory if yii has cross platform substitution for it ?

I found an example Yii 1.1: How to upload a file using a model ( http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/ ). But in my case uploaded image has no any relation to any database and no related table.field. How to upl;oad image in this case ?