Jump to content
westman

photo files

Recommended Posts

I have files that users upload.
1.jpg 2.jpg 3.jpg etc.
what if the best way of finding out what _.jpg are in a folder as users may delete an image.

Share this post


Link to post
Share on other sites

There are many ways to look at the contents of a directory using PHP.

I don't see a need to use numbers as  filenames, overall it actually becomes more difficult to manage because conflicts may occur when two or more users are creating files at the same time.

Share this post


Link to post
Share on other sites

This is how I started the needed code but it is not working

$directory = 'pro_images/'.$id.'/casual';
if (file_exists($directory) && is_dir($directory)) {
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
}
$scanned_directory2 = explode(",", $scanned_directory);
echo $scanned_directory2;

any help?

Share this post


Link to post
Share on other sites

$scanned_directory is already an array so you cannot use explode () on it. Perhaps you intended to use implode ()

Share this post


Link to post
Share on other sites

I found a method, coded it and it works.
Every image uploaded I added to an array I put in the DB.
Looking like this; 1,2,3,4,5,7,8,9
To view the images I used this...

$pic_array = explode(",", $pic);
foreach ($pic_array as $key => $value) {
$check_pic_array = 'pro_images/'.$id.'/pro'.$value.'.jpg';
if (file_exists($check_pic_array)) {
$pic = '<img src="$check_pic_array" style="max-width:100px; max-height:100px;" border="1">';

}
}


As you can see number 6 is missing. With this method it is easy to locate and remove unwanted files from the DB and server.

Thank you for your help. 

Share this post


Link to post
Share on other sites

Just got to the removing stage of my coding.
How do I find number 6 in my array and remove it?

$pic_array = "1,2,3,4,5,6,7,8,9";
$remove = "6";
if (in_array($remove, $pic_array)) { 
// ???
}

 

Share this post


Link to post
Share on other sites

There are several ways to do this as explained here https://stackoverflow.com/questions/369602/php-delete-an-element-from-an-array?rq=1

Since you know the value/s you might as well use array_diff()

        <?php
        $pic_array = [1, 2, 3, 4, 5, 6, 7, 8, 9];

        echo '<p>remove key/value, but not reseting remaining keys using array_diff()</p>';
        $remove = [6];
        $pic_array = array_diff($pic_array, $remove);
        var_dump($pic_array);

        echo '<p>remove multi keys/values, but not reseting remaining keys using array_diff()</p>';
        $remove = [6, 2];
        $pic_array = array_diff($pic_array, $remove);
        var_dump($pic_array);

        echo '<p>reseting remaining key values using array_values()</p>';
        $pic_array = array_values($pic_array);
        var_dump($pic_array);
        ?>

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you so much for the examples.
few more questions.
In my DB my array looks like
1,2,3,4,5,6,7,8,9
not
1, 2, 3, 4, 5, 6, 7, 8, 9
Is that a problem?

also 
can I change

$remove = [6];

to
 

$number_to_drop = '6';
$remove = [$number_to_drop];

?

 

Share this post


Link to post
Share on other sites

1) No, that is not a problem, you could have checked this, with my example.

2) A long as it is treated as a array, no it should not be a problem, but doing it as multiple string values is more complicated.

Share this post


Link to post
Share on other sites

Last question on this.

 

$pic_array = array_diff($pic_array, $remove);
var_dump($pic_array);

is printing the following to my page
array(5) { [0]=> string(1) "1" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" }

How do I stop it printing to my page?

Share this post


Link to post
Share on other sites

Did you even consider/think of just commenting out the last line to see what would happen? OR did you think it would cause launching of nuclear weapons from silos, ending life on this planet as we know it!

  • Haha 1

Share this post


Link to post
Share on other sites

lol, that's funny.

it is the last line...

var_dump($pic_array);

that is printing to the page.

How do I stop it and still have the code working?

Edited by westman

Share this post


Link to post
Share on other sites

ALL that does is show you what is in the array, nothing more, nothing less! So using your little grey cells, WHAT do you think you should do?

(A) Remove.

(B) Keep.

(C) Go to bar, have a couple of drinks and hope it goes away, by the time you get back.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×