Jump to content

photo files


westman

Recommended Posts

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.

Link to comment
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?

Link to comment
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. 

Link to comment
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
Link to comment
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];

?

 

Link to comment
Share on other sites

  • 2 weeks later...

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?

Link to comment
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.

Link to comment
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
×
×
  • Create New...