Yessie Setiawan
Yessie Setiawan

Reputation: 105

get image from folder outside yii

EDIT

My htdocs/myprojectyii folder is in C drive, So can I get image from folder in E drive? I edit my coding to this

<?php $path = 'file:///E:/image/myimage.jpg'?>
<img src="<?php echo $path ?>" width="100" height="100"/>

if i click path on src image (firebug), path is right(cause it display image) but in mypage, image not display

Upvotes: 1

Views: 1603

Answers (3)

Yessie Setiawan
Yessie Setiawan

Reputation: 105

@Mostafa Shahverdy and @Kalpesh Patel thank you so much for your supports.. but finally i got solution to use base64_encode, like this

$path = 'E:/images/myimage.jpg'

    if($fp = fopen($path,"rb", 0)) //open file
    { 
        $path = fread($fp,filesize($path)); //read file
        fclose($fp); 
        $path = chunk_split(base64_encode($path)); //encode image to base64
        $encode = '<img src="data:image/jpeg;base64,' . $path .'"  width="200" height="200">'; 
        echo $encode; //show image
    }

so i can access image in local drive :)

Upvotes: 0

Kalpesh Patel
Kalpesh Patel

Reputation: 40

For better security yii not allow direct but you can use php function to get images

see in yii controller names UploadController.php add function

function actiongetBusinessProfilePic($dir='',$fileName='')
{
    $name = FILE_UPLOAD.'businessprofilepic/'.$dir.'/'.$fileName;
    $ext=$this->actiongetExt($fileName);
    if(file_exists($name))
    {
    $fp = fopen($name, 'rb');   
    header("Content-Type: image/".$ext);
    header("Content-Length: " . filesize($name));
    fpassthru($fp);
    }
}

now in view page do sometihng like this

<img id="main_image_preview" src="<?php echo $base_path;?>upload/getBusinessProfilePic/dir/<?php echo $imageDir;?>/fileName/<?php echo $avatar;?>" alt="Upload Avatar" border="0" />

Upvotes: 0

Mostafa Shahverdy
Mostafa Shahverdy

Reputation: 2725

First of all you must try file:///E:/images/myimage.jpg instead of only E:/image/myimage.jpg. Second this won't work :D because That's a security feature .. it isn't possible in any browser on any platform.

Upvotes: 1

Related Questions