phpfilepropertiesfileinfo

How to read file properties with PHP?


I'm wondering if there is a way I can store data like the author, in a file? Like you right click on a file in Windows, and you set properties of it. Can I read those properties in PHP ?

What I really want to do is, I want to upload images to a directory, and when I'm showing the images in the PHP page, I want to get the alt attribute directly from the jpg (or png) file's "properties".

I have tried finfo_file() function of PHP, but no success.

Thanks for any help.


Solution

  • I'm wondering if there is a way I can store data like the author, in a file? Like you right click on a file in Windows, and you set properties of it

    There are two ways that Windows can show properties for a file. The first way is if it knows about the file format and the file format can contain metadata. Examples of this are JPEGs and Word documents. In both of those formats metadata about the file (author, etc) are contained within the file, and Windows knows how to show it. The second way metadata can be stored is in Alternative Data Streams (ADS) with an NTFS file system. A good guide to using ADS can be found at http://www.irongeek.com/i.php?page=security/altds

    Can I read those properties in PHP ?

    For in-file metadata it depends on the file format. JPEG EXIF data can be read by the exif-read-data function (http://php.net/manual/en/function.exif-read-data.php)

    I don't have a handy NTFS partition to test this on, but I believe you might be able to access Alternative Data Streams within PHP if running it in Windows and accessing an NTFS file system.

    What I really want to do is, I want to upload images to a directory, and when I'm showing the images in the PHP page, I want to get the alt attribute directly from the jpg (or png) file's "properties".

    JPEGs that contain the metadata you want can be read by the exif-read-data function as mentioned above. PNGs don't hold EXIF data so there is no respective function for that (PNGs can contain metadata, but there doesn't seem to be a standard to it).

    If you get the user to include name and other data when they upload the file you could store that and retrieve it when showing the image. The data could be stored anywhere, e.g. in a database (perhaps linking file name to data), or on the filesystem (for an image called image1.png you could store the author's name in a file called image1.png.txt).