westman Posted April 28, 2018 Share Posted April 28, 2018 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. Link to comment Share on other sites More sharing options...
Ingolme Posted April 28, 2018 Share Posted April 28, 2018 There are many ways to look at the contents of a directory using PHP. scandir() opendir() DirectoryIterator glob() 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 More sharing options...
westman Posted April 29, 2018 Author Share Posted April 29, 2018 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 More sharing options...
Ingolme Posted April 29, 2018 Share Posted April 29, 2018 $scanned_directory is already an array so you cannot use explode () on it. Perhaps you intended to use implode () Link to comment Share on other sites More sharing options...
westman Posted April 29, 2018 Author Share Posted April 29, 2018 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 More sharing options...
westman Posted April 30, 2018 Author Share Posted April 30, 2018 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)) { // ??? } Link to comment Share on other sites More sharing options...
dsonesuk Posted April 30, 2018 Share Posted April 30, 2018 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); ?> 1 Link to comment Share on other sites More sharing options...
westman Posted April 30, 2018 Author Share Posted April 30, 2018 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 More sharing options...
dsonesuk Posted April 30, 2018 Share Posted April 30, 2018 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. Link to comment Share on other sites More sharing options...
westman Posted April 30, 2018 Author Share Posted April 30, 2018 thank you so much. Link to comment Share on other sites More sharing options...
westman Posted May 13, 2018 Author Share Posted May 13, 2018 Last question on this. $pic_array = array_diff($pic_array, $remove); var_dump($pic_array); is printing the following to my pagearray(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 More sharing options...
dsonesuk Posted May 13, 2018 Share Posted May 13, 2018 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! 1 Link to comment Share on other sites More sharing options...
westman Posted May 13, 2018 Author Share Posted May 13, 2018 (edited) 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 May 13, 2018 by westman Link to comment Share on other sites More sharing options...
dsonesuk Posted May 13, 2018 Share Posted May 13, 2018 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 More sharing options...
westman Posted May 13, 2018 Author Share Posted May 13, 2018 lol. got it! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now