File Removal after Database Comparison
File Removal after Database Comparison
am 03.04.2008 23:33:02 von Chris Owens
Hi all,
I'm trying to figure the logic (and therefor the code) to enable the
clean-up of a site I've inherited. Basically I have a folder that has
around 6,666 images in it that are loosely related to the information
held in a database. I say loosely because the images are uploaded (via
FTP) manually. There is no direct reference to the JPEGs in the
database, as the code on the site references them via:
Each record in the database has three related images:
Image of the front - recordid_f.jpg
Image of the side - recordid_s.jpg
Image of the back - recordid_b.jpg
Now over time records have been removed from the database, but the
images haven't been removed and as such, short of removing all images,
there is no easy way that I can think of to clear the folder (several
GB of data) whilst keeping the images that have related records in the
db.
My question is: How can I through code remove any orphaned images (that
aren't needed anymore), whilst keeping the images that still relate to
records in the database?
Any assistance/pointers/snippets would be much appreciated.
Kind regards,
Chris
Re: File Removal after Database Comparison
am 04.04.2008 00:43:21 von sandy_saydakov
On Apr 3, 2:33 pm, Chris Owens wrote:
> Now over time records have been removed from the database, but the
> images haven't been removed and as such, short of removing all images,
> there is no easy way that I can think of to clear the folder (several
> GB of data) whilst keeping the images that have related records in the
> db.
>
> My question is: How can I through code remove any orphaned images (that
> aren't needed anymore), whilst keeping the images that still relate to
> records in the database?
Are you looking for an algorithm? What about the following:
read the directory and get list of files
for each file in the list {
if file is not in the database {
remove file
}
}
/sandy
http://myphpquiz.com/
Re: File Removal after Database Comparison
am 04.04.2008 02:00:45 von Chris Owens
On 2008-04-03 23:43:21 +0100, Sandy said:
> On Apr 3, 2:33 pm, Chris Owens wrote:
>
>> Now over time records have been removed from the database, but the
>> images haven't been removed and as such, short of removing all images,
>> there is no easy way that I can think of to clear the folder (several
>> GB of data) whilst keeping the images that have related records in the
>> db.
>>
>> My question is: How can I through code remove any orphaned images (that
>> aren't needed anymore), whilst keeping the images that still relate to
>> records in the database?
>
> Are you looking for an algorithm? What about the following:
>
> read the directory and get list of files
> for each file in the list {
> if file is not in the database {
> remove file
> }
> }
Hi Sandy, thanks for chipping in on this.
I should have made myself clearer, as I don't think I explained my
problem very well.
The bit that I'm struggling with is...
if file is not in the database {
remove file
}
I have the list of files in the directory in an array (called $files).
In the array, 3 images for every db entry (and 3 images for missing
database entries also) are stored with the appropriate "_f.jpg",
"_s.jpg", "_b.jpg" after each image. It is at this point that I'm lost
and not sure what to do next.
I know that I need to go through the array and check to see if an entry
with the same recordid exists, but I'm not sure how to get the recordid
from the filenames (as they also have _b.jpg etc in them). If an entry
exists I leave the image, if it doesn't I delete the 3 corresponding
images.
Thanks again,
Chris
Re: File Removal after Database Comparison
am 04.04.2008 02:38:49 von sandy_saydakov
On Apr 3, 5:00 pm, Chris Owens wrote:
> I know that I need to go through the array and check to see if an entry
> with the same recordid exists, but I'm not sure how to get the recordid
> from the filenames (as they also have _b.jpg etc in them). If an entry
> exists I leave the image, if it doesn't I delete the 3 corresponding
> images.
Now we are talking. If I understand you right, the record ID can be
obtained by chopping off the last 6 characters of the fie name:
$file = '123_f.jpg';
$recordid = substr($file, 0, strlen($file) - 6);
/sandy
http://myphpquiz.com/
Re: File Removal after Database Comparison
am 04.04.2008 08:42:52 von Courtney
Chris Owens wrote:
> On 2008-04-03 23:43:21 +0100, Sandy said:
>
>> On Apr 3, 2:33 pm, Chris Owens wrote:
>>
>>> Now over time records have been removed from the database, but the
>>> images haven't been removed and as such, short of removing all images,
>>> there is no easy way that I can think of to clear the folder (several
>>> GB of data) whilst keeping the images that have related records in the
>>> db.
>>>
>>> My question is: How can I through code remove any orphaned images (that
>>> aren't needed anymore), whilst keeping the images that still relate to
>>> records in the database?
>>
>> Are you looking for an algorithm? What about the following:
>>
>> read the directory and get list of files
>> for each file in the list {
>> if file is not in the database {
>> remove file
>> }
>> }
>
> Hi Sandy, thanks for chipping in on this.
>
> I should have made myself clearer, as I don't think I explained my
> problem very well.
>
> The bit that I'm struggling with is...
>
> if file is not in the database {
> remove file
> }
>
> I have the list of files in the directory in an array (called $files).
>
> In the array, 3 images for every db entry (and 3 images for missing
> database entries also) are stored with the appropriate "_f.jpg",
> "_s.jpg", "_b.jpg" after each image. It is at this point that I'm lost
> and not sure what to do next.
>
> I know that I need to go through the array and check to see if an entry
> with the same recordid exists, but I'm not sure how to get the recordid
> from the filenames (as they also have _b.jpg etc in them). If an entry
> exists I leave the image, if it doesn't I delete the 3 corresponding
> images.
>
> Thanks again,
>
> Chris
>
Why not write some code that simply takes all the files that are valid,
(that have corresponding records) and moves them into a temporary new
directory: then delete the old directory with its contents, and move teh
new directory to its old name.
Oh, and this time modify the database to include the images in it...
Re: File Removal after Database Comparison
am 08.04.2008 01:43:13 von Chris Owens
On 2008-04-04 07:42:52 +0100, The Natural Philosopher said:
> Chris Owens wrote:
>> On 2008-04-03 23:43:21 +0100, Sandy said:
>>
>>> On Apr 3, 2:33 pm, Chris Owens wrote:
>>>
>>>> Now over time records have been removed from the database, but the
>>>> images haven't been removed and as such, short of removing all images,
>>>> there is no easy way that I can think of to clear the folder (several
>>>> GB of data) whilst keeping the images that have related records in the
>>>> db.
>>>>
>>>> My question is: How can I through code remove any orphaned images (that
>>>> aren't needed anymore), whilst keeping the images that still relate to
>>>> records in the database?
>>>
>>> Are you looking for an algorithm? What about the following:
>>>
>>> read the directory and get list of files
>>> for each file in the list {
>>> if file is not in the database {
>>> remove file
>>> }
>>> }
>>
>> Hi Sandy, thanks for chipping in on this.
>>
>> I should have made myself clearer, as I don't think I explained my
>> problem very well.
>>
>> The bit that I'm struggling with is...
>>
>> if file is not in the database {
>> remove file
>> }
>>
>> I have the list of files in the directory in an array (called $files).
>>
>> In the array, 3 images for every db entry (and 3 images for missing
>> database entries also) are stored with the appropriate "_f.jpg",
>> "_s.jpg", "_b.jpg" after each image. It is at this point that I'm lost
>> and not sure what to do next.
>>
>> I know that I need to go through the array and check to see if an entry
>> with the same recordid exists, but I'm not sure how to get the recordid
>> from the filenames (as they also have _b.jpg etc in them). If an entry
>> exists I leave the image, if it doesn't I delete the 3 corresponding
>> images.
>>
>> Thanks again,
>>
>> Chris
>>
>
> Why not write some code that simply takes all the files that are valid,
> (that have corresponding records) and moves them into a temporary new
> directory: then delete the old directory with its contents, and move
> teh new directory to its old name.
>
> Oh, and this time modify the database to include the images in it...
Thanks NP,
I went with this method, and it works a treat.
Regards,
Chris