1

If I grab data from a column in my database called image_store such as:

/web/images/data/firstImage.png
/web/images/data/secondImage.png
/web/images/data/thirdImage.png

And I want to populate a setImage function which takes the string after /data/ to set as the fileName and the string before /firstImage.png as the directory e.g:

$this->setImage('firstImage.png', '/web/images/data/')

Is there a way I can grab the needed information from the string I get back from the database?

Sudoscience
  • 301
  • 3
  • 11
  • Possible duplicate of [How to get file name from full path with PHP?](http://stackoverflow.com/questions/1418193/how-to-get-file-name-from-full-path-with-php) – Laur Ivan Mar 31 '16 at 09:21

1 Answers1

1

Yes, simply use explode/implode functions and array_pop() to take image name itself:

<?php
    $sqlArr = array('/web/images/data/firstImage.png', '/web/images/data/secondImage.png', '/web/images/data/thirdImage.png');
    foreach($sqlArr as $fullPath){
        $tmp = explode('/', $fullPath);
        $imgName = array_pop($tmp);
        $imgPath = implode('/', $tmp).'/';
        $this->setImage($imgName, $imgPath);
    }
?>

This will works with any depth of url paths.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31