PHP: Determine if file is on removable media

43 Views Asked by At

My current project, written in PHP, requires that certain extraneous file be removed from a volume, if that volume is removable.

The project scans for JPEG and MPEG files, and moves them from the specified source to the specified target (with renames according to metadata and time).

An example might be:

/Volumes/volume/DCIM/100MEDIA/IMG00005.JPG -> /Target/Dir/Boston-230403-0005.jpg

Once the task is done, the DCIM/100MEDIA folder (in this case) is removed if empty. The new project requirement is that, if (e.g.) /Volumes/volume is removable media, then also remove the following folders and their content if they exist:

(root)/.Trashes
(root)/.Spotlight-V100
(root)/.fseventsd

In general, the application is used to move files from removable media to fixed (a smart SD card unloader), but it does not always have to be used that way. However, the new requirement dictates that the removal of those folders (.Trashes, .fseventsd, .Spotlight-V100) must be done only against removable media.

Hence the question: Is there a reasonable and reliable method available to determine if a named file represents removable media? fstat() will reveal the device number (['dev'] element), and that is probably the needed hook... but from that, can we get to the type of device? And will such device type tell us what we need to know?

A klunky alternative is, rather than looking at device type, to look for '/DCIM/' in the pathname, and to remove those folders from that same point in the path, a la:

/Volumes/media/DCIM/Camera_01
/Volumes/media/.Trashes
/Volumes/media/.fseventsd
/Volumes/media/.Spotlight-V100

It just feels like things may go wrong with that approach...

NOTE: Mac users may recognize those names, and [correctly] assume this is being done on a Mac. While a portable solution is desired, a Mac-specific solution may be necessary. That's OK in this instance.

Thanks.

0

There are 0 best solutions below