5

I am inserting multiple images on server and storing there name in SQL database by (,) seperated using this.

if($request->hasFile('images')){
     $images= [];
        foreach($images=$request->file('images') as $img) {
             $name=$img->getClientOriginalName();
             $img->move(public_path().'/dpic', $name);    

            $images[]=$name;
        }

    }
            $test =implode(", ", $images);   
            $product->images  =$test;

Image name are inserting into database along with some data it shows output like.

/tmp/php59iuBb, /tmp/phpdRewVH, PicturesI.jpg, Screenshot.png

I want to remove this /tmp/php59iuBb, /tmp/phpdRewVH from output How can I do that.

please guide me to do so.

1
  • 1
    It is a good question. but you shouldn't store original filename from client into database. why? because that an override problem when you persist your upload file on disk in original name. in case, two users upload the different file and the same filename. existed file will replace. the best way, create a new filename for image based on current datetime that make sure in unique name with original extension before you save on disk and sql. Commented Mar 13, 2019 at 8:05

4 Answers 4

6

I would do this

$images =[
    '/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'
];

$images = preg_grep('~^(?!/tmp/)~', $images);

print_r($images);

Output

Array
(
    [2] => PicturesI.jpg
    [3] => Screenshot.png
)

Sandbox

Simple right!

Preg grep runs a regular expression against an array and returns the matches.

In this case

  • ~^(?!/tmp/)~ negative lookbehind - insures that the match does not start with /tmp/

Which leaves us what we want.

Another option is

 $images = array_filter($images,function($image){
               return substr($image, 0, 5) != '/tmp/';
           });

If you are not feeling the Regex love.

Sandbox

PS I love preg_grep its often overlooked for easier to understand but much more lengthy code. Preg Filter is another one of those, which you can use to prefix or suffix an entire array. For example I've used it to prepend paths to an array of filenames etc. For example it's this easy:

$images =[
    '/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'
];

print_r(preg_filter('~^(?!/tmp/)~', '/home/images/', $images));
//or you can add a whole image tag, if you want, with a capture group (.+) and backrefrence \1
print_r(preg_filter('~^(?!/tmp/)(.+)~', '<img src="/home/images/\1" />', $images));

Output

Array
(
    [2] => /home/images/PicturesI.jpg
    [3] => /home/images/Screenshot.png
)

Array
(
    [2] => <img src="/home/images/PicturesI.jpg" />
    [3] => <img src="/home/images/Screenshot.png" />
)

Sandbox

I thought you may find that "trick" useful as you can remove the bad ones and add a path to the good at the same time. They are worth checking out.

http://php.net/manual/en/function.preg-grep.php

http://php.net/manual/en/function.preg-filter.php

I feel like I should mention the same holds true for matching a file extension, which may also be useful, but I will leave that for another day.

Cheers!

Sign up to request clarification or add additional context in comments.

2 Comments

Two little things: 1. I would use /^(?!\/tmp\/)/ over /^(?!\/tmp).+/ (no need to match the rest of the string, and better to add a slash to match exactly /tmp/ and not /tmperature/ and 2. I would use the same regex in the last example to better showcase preg_filter, otherwise it behaves exactly like a preg_replace would (since it just matches everything) and returns the unwanted paths as well.
@Jeto - those are both excellent points, Updated. It's late here, I'm surprised I missed the /tmperature/, I also fixed array_filter for the same issue - I know Regex really well, except the look arounds. I been trying to practice them actually. I just assumed I would need to match something in the string... Thanks for pointing that out! I also changed the delimiters for obvious reasons. I'm slipping in my old age.
3

Bit late to the party, but I would personally prefer using pathinfo over regular expressions here, since it's dedicated to file paths:

$images = ['/tmp/php59iuBb', '/tmp/phpdRewVH', 'PicturesI.jpg', 'Screenshot.png'];

$images = array_filter($images, function ($image) {
  return pathinfo($image, PATHINFO_DIRNAME) !== '/tmp';
});

print_r($images);

Demo: https://3v4l.org/6F6K8

Comments

1

I will go with this way, hope this helps you.

$images= [];

if($request->hasFile('images')){

       foreach($request->file('images') as $img) {

          $name = "some_random_sting";

          $extension = $img->getClientOriginalExtension();

          $imgName = $name .'.'.$extension;

          $img->move(public_path().'/dpic', $imgName);    

          $images[] = $imgName;
       }

}

$test = implode(", ", $images);   
$product->images  = $test;

Comments

0

The foreach loop is statement also storing temp path of image to $images

Change variable name in foreach

$images=$request->file('images') tto $image=>$request->file('images')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.