How to Get File Information in PHP

Cloudmersive
2 min readApr 8, 2024

--

Using a few lines of PHP, we can easily call a free API to get key information about a variety of common file types.

We can run this file check on over 100 unique image formats, all common Office file formats, PDF documents, and more.

We’ll return information about the file (as much as is available) structured like the JSON example below:

{
"Successful": true,
"DetectedFileExtension": "string",
"DetectedMimeType": "string",
"PageCount": 0,
"Author": "string",
"DateModified": "2024-04-08T13:57:46.633Z",
"AlternateFileTypeCandidates": [
{
"Probability": 0,
"DetectedFileExtension": "string",
"DetectedMimeType": "string"
}
]
}

To structure our API call, we can begin by installing the client SDK. Let’s execute the below command from our command line to install with Composer:

composer require cloudmersive/cloudmersive_document_convert_api_client

Next, let’s copy the below code examples to call the function. We can provide our file path in the $input_file variable, and we can supply a free Cloudmersive API key to authorize our requests (this allows up to 800 API calls per month with no commitments):

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\ConvertDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.

try {
$result = $apiInstance->convertDocumentAutodetectGetInfo($input_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertDocumentApi->convertDocumentAutodetectGetInfo: ', $e->getMessage(), PHP_EOL;
}
?>

That’s all there is to it — now we can easily get key information about a variety of file types with a quick API call.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet