skip to Main Content

I am trying to open the raw disk on Windows/PHP. (Like /dev/sda on Linux.)

I tried (as admin):

fopen("\\?\Device\Harddisk1\Partition0");

I have this path from here. Yes, it’s the whole disk and not a partition.

But I get:

 fopen(\?DeviceHarddisk1Partition0): Failed to open stream: No such file or directory

I am using PHP 8.2.10.

I also tried these paths but I get the same error:

\?DeviceHarddisk1Partition1
\?DeviceHarddisk0Partition0
\?DeviceHarddisk0Partition1
\?DeviceHarddisk0
\?DeviceHarddisk1

If this is not possible in PHP I’d welcome an explanation why exactly it’s not supported.
I am writing my own data recovery script. It’s already running well on Linux but I want to port it to Windows.

2

Answers


  1. Fundamentally this is a windows issue.

    Windows doesn’t work like this.

    Login or Signup to reply.
  2. Congratulations on accidentally discovering a bug in PHP, it lies in this line:

    #ifdef ZEND_WIN32
        if (memchr(resolved_path, '*', path_length) ||
            memchr(resolved_path, '?', path_length)) { // <<<<<<<<<<<<<
            SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_NAME);
            return 1;
        }
    #endif
    

    Obviously if the path contains a ?, php will consider this to be an invalid path.

    This is quite reasonable, and I don’t think it is worth fixing, because Windows creates aliases for each partition, e.g.

    \?DeviceHarddiskVolume1
    \.Volume{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
    \.c:
    

    You can use fopen to open the next two forms (administrator privileges required).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search