2

I knew that PHP is able to read file content by different ways, for example: fread, file_get_contents, file, readfile, etc.

Currently, I am looking for an API that can read real index of files and folders in specific partition or folder, for example: drive d:\ in windows contains three folders (folder1, folder2, folder3), and each folder contains some files, we can get these directory structure using PHP (opendir, scandir, readdir, etc) and list them as I want, however, windows saved file and folder names inside hard-disk with their attributes (size, last modified, created on, etc).

How I can read hard-disc using PHP and retrieving all file and folder attributes for a specific path? for instance, if we consider last modified time we can use (filemtime()) function, but this attribute not saved inside the file, its saved some where else inside hard-drive, other attributes also saved in other location not inside the file.

When windows user copying file from flash-drive to local hard, windows will copy all file and folder attributes and saves them inside local hard drive. When using PHP for copying file, it depends on OS to handle this job, its not native support (as I think) for file and folder operations.

Do you have any idea? There are many recovery program that uses this technology for reading hard-drive indexes, however, for PHP: I cant find any source for this problem.

Applications if I get correct answer: I can check if such file securely deleted from my hard-drive? I can create secure delete application using PHP, or clearing hard-drive indexes for a given file.

Your help appreciated.

  • I'm not sure if I understand what your goal is. Do you really want to use PHP for file recovery? Is there a specific reason for it? For file synchronization there are good command line tools like `rsync` on Linux and `robocopy` on Windows, you could use them from PHP via `exec()` if you must. – Fabian Schmengler Jan 29 '13 at 12:07
  • @fab: I just want to be able to read real location of file and folder index table using PHP –  Jan 29 '13 at 12:34
  • Just curios why you need the real index and why php as a choice for this task ? – Baba Feb 02 '13 at 15:59
  • @Baba because its easy to implement it with current Security Application that I use it for auditing. –  Feb 02 '13 at 16:15

3 Answers3

2

Problems with the proposition

The attributes of files, such as timestamps, permission flags etc, are stored in the file system (FAT, NTFS, Ext3 etc). As you say some of them can be read using PHPs different file and directory methods, but they all act through the OS file system abstraction and cant have access to block level information on the disk, such as what precise byte on disk stores the archive flag for file X. The whole point of the OS and FS is to abstract away this information from the user/client programs.

As suggested there are external tools, written in c or similar, that does have this access and that you can call from inside PHP. If you want a 'native' PHP way of doing this you'll have to compile a c extension for PHP that exposes these low level functions to you.

I'd say external tools is the way to go if you want to stick with PHP but for the task at hand, as far as we can see from your description, I'd go with another language that has more low level access. Like C or C++. PHP is a high level language for HTML pre processing and as such is a poor choice for low level system programming.

Practical advice

After looking through the PHP documentation and assorted third party libraries:

An of the shelf solution for reading file system information on a file allocation table level doesn't exist for PHP. The lowest level you get is the fstat() function, and that is not very far for what you want.

External tools

No mater exactly what you want to do there is probably a small binary that does it. PHP can be integrated with these programs, as suggested elsewhere, via the exec() function. This is probably the easiest approach for you unless you have serious amounts of time and/or development resources to devote to this problem.

Wrapping a library

There are libraries that solves this problem for you, written in low level languages. An open source library can be wrapped with SWIG to expose it to PHP. This will give you access to the low level methods you need, but it's a non trivial task. These kind of libraries also often require sole access to the device while they work on it, something that is difficult to achieve in most normal operating environments.

Note also that you will probably need a library per file system. Microsofts VFAT extension to FAT12/16/32 requiers a licens to use. So if you want to work with FAT and have files with long names (not 8.3 format) you'll have to fork up some dough to be legit.

Low level implementation

A last middle ground would be to write your own CLI tool that uses an external library to access the low level FS functions. You can then use exec() from inside PHP to interact with your own implementation.

This might be a reasonable path if you cant find an existing tool that solves your problem and you are not willing to spend the time to wrap a library.

In closing

You give a very narrow problem description with little to go on as for what the application is about. A broader discussion (in another forum) might yield better results since the problem might be better solved in another way entirely.

0

I found something on PHP.net which appears to do what you want:

http://php.net/manual/en/function.readdir.php#103418

Edit: I mis-understood the question. Attributes such as the last modified time, last accessed date and the like are stored in the file systems master file table. As far as I can tell, this isn't accessible with PHP, and if you were to write your own method to do this then you'd also have to account for different file systems as they all handle the storage of these attributes in their own unique way.

It could be that to get all of the information you're looking for is not possible with PHP without writing some form of extension to PHP itself.

Edit 2: Upon researching a little more...

http://php.net/manual/en/function.fileinode.php

This function could be an interesting one to look at.

Seer
  • 5,226
  • 5
  • 33
  • 55
  • I mentioned in my question that I knew about these functions, my aim is to read hard drive index of which the file names and attributes were saved. –  Jan 29 '13 at 12:06
  • My apologies, I misinterpreted the question, I've updated my answer with the extend of my knowledge on this subject. – Seer Jan 29 '13 at 12:16
  • I've updated the answer again with a little bit more, what are all of the attributes that you want? – Seer Jan 29 '13 at 12:43
  • 1
    The inode number is basically a logical block in ext and other unix filesystems. It's not available on NTFS or FAT/FAT32 nor does it give you any information on where that file is stored on the physical device, files keep their inode numbers when moved. – Jonas Schubert Erlandsson Jan 29 '13 at 12:57
0

Well if I understand correctly you just want to securely delete a file. You can just call [shred][1]

[1]: http://linux.die.net/man/1/shred via system or exec if you are on linux and you are good to go

tix3
  • 1,142
  • 8
  • 17
  • Thanks, but not by this way, I need a real API that can be used with other programming language, as @d-Pixie explained that, this might be possible with low level access programming languages such as C. –  Jan 31 '13 at 05:02