0

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?

Edited by JorgeM: Move to PHP forum.

4
Contributors
3
Replies
17
Views
3 Years
Discussion Span
Last Post by steelbrain
1

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.

0

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.

0

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
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.