How do one go about opening a file in php by using the resources made available on a system.
Take for instance that after getting the header content information of a file, one discovers that the file is a word document, or a pdf file, how do one use a php available functions to get to alert the system to open the file based on the type of file you are dealing with?

Recommended Answers

All 3 Replies

I like the way how one ask his question. He reminded me of the Bicentennial Man my favorite movie when I was a kid.

To answer one's question, one should find the extension of the file and then based on the file extension one should execute a function to do whatever one's wishes.

What about opening the file in a new tab or something, and therefore it will download to the user automatically. Of course, you would need to use some alert messages to check your user is okay with downloading a .doc file etc.

If you want to show the contents of these files in your PHP application, that suddenly complicates things a lot.

From what I've experienced so far, You can read the file's first few bytes, convert them to hex and then compare to a similarity you found between the same type of files.
I've used this to identify png vs apng, webp animated vs webp non animated and gif animated vs gif non-animated.
For this of course you'll have to find some bytes that are same among all the files of the type on the specific number.

For example:

<?php
$fr = fopen($this->source_path,"r");
fseek($fr,9);
$headers = (string)bin2hex(fread($fr,7));
fclose($fr);

if($headers === '45425056503820'){
    // It's a non-animated webp
} else {
    // It's an animated webp
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.