File validation with header signature in PHP - validation

Suppose i have one file a.png. In php we will validate it with extension & mime type.
In form i have make above validation so only png files are going to upload. If i make xyz.pdf to a.png it's going to valid.
I would like to validate file with header signature as in .NET.
Also while upload if image is broken then also it gives the message image is corrupted unable to upload.
In PHP we have to use exif_imagetype ? It's only for image what about files like video, audio or any file ?

For png images in php, you can also use createimagefrompng it returns FALSE if it is not a valid png image file.

You can determine the mime type of it. PHP mime_content_type will work on this but it is already deprecated in PHP 5.3. You can use file_info_file into it.
Take a look at this link for a more detailed explaination:
http://www.php.net/manual/en/function.mime-content-type.php
http://us2.php.net/manual/en/function.finfo-file.php

Related

Laravel: Imagick and GD error at AWS Lambda with Bref

I set up an laravel application at AWS Lambda, using Bref. Everything works fine, including filesystem and s3 filestorage. I use spatie's medialibrary to handle file upload and media conversions, and I can upload a file without any troubles.
The problem appear when I try to make image conversions, using either GD or Imagick.
Whenever I try to make the conversions, I get the following errors:
When using Imagick:
Intervention\Image\Exception\NotReadableException
Unable to read image from path (/tmp/Glide0PSwRU).
When using GD:
Intervention\Image\Exception\NotReadableException
Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files.
This only happens when running the application at AWS Lambda. If I run at my local environment or at another service like Google App Engine, with the "same" code configs (related to the upload/media conversion).
Create a php/conf.d/php.ini in your project
then add extension=imagick or extension=gd to enable these extensions
You can read the docs here

How to download file using ProtoBuf

I'm trying to implement file download directly via Browser. Our company uses Protocol Buffer as data communication format. So how can I download the file once I open the web page?
I tried to user bytes and stream of Protocol Buffer. But the result is
{"result":{"data":"Cw4ODg4ODgsMCw4ODg4ODgsMTUwMCwwLDE1MDAsNDAwMDAsMTAwMDAsMzAwMDAKMDMvMTEvMjAxNSxVbmtub3duIEl0ZW0sUHJlIFJvbGwgVmlkZW8gKG1heCAwOjMwKSw2MDAwMCwzMTAwMCwyOTAwMCw1MDAwMCwyNDAwMCwyNjAwMCwyMC4wMCUsODQ0NCwwLDQwMDAsNDQ0NCw4OTAzODgsMCwwLDAsODg4ODg4LDAsODg4ODg4LDE1MDAsMCwxNTAwLDQwMDAwLDIxMDAwLDE5MDAwCg=="}}
Protobuf is good for structured communication but http provides the perfect protocol for downloading files. The right headers need to be set and the browser will download the file.
If you really have to use protobuf to transfer files, then you need to add some javascript that is parsing the protobuf first and then turns it into a file that can be downloaded. See How to create a dynamic file + link for download in Javascript? for reference.
So, send the message as bytes, add the javascript that parses the protobuf message to extract the bytes, and then create the file download like on the linked answer.

InlineQueryResultArticle - thumb_url uploaded image

Is there an option to use an image uploaded to telegram (file_id couldn't do it) inside InlineQueryResultArticle thumb_url?
notes:
I tried to get the file path using getfile() but it didn't do the trick.
even tried to upload a very small image size with no luck.
I'd like to ignore the InlineQueryResultCachedPhoto option since the design is not the same
any thoughts?
thank you!
I also checked that the telegram does not show the uploaded image, I think it is better to give the address of the picture directly.

PHPExcel Load image from outside URL

Does anyone know how to make setPath() method able to load an image from outside server? Because all images is store on other server.I don't have any idea how to do that. Please help me. thanks
<pre>
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('http://domain.com/images/testing.jpg');
$objDrawing->setHeight(96);
$objDrawing->setOffsetX(27);
$objDrawing->setOffsetY(40);
$objDrawing->setCoordinates('A9');
$objDrawing->setWorksheet($this->excel->getActiveSheet());
</pre>
Images can't be referenced from a URL, you need the image in your local filesystem because PHPExcel needs to extract information from that image. Use curl (or even file_get_contents()) to pull the image to the local filesystem first. Once the image has been embedded in the Excel document, you can delete the file again.
You can put false as the second option in the setPath() method. Tcpdf gets the image from the URL and PHPExcel doesn't throw an exception.

Detecting file type

What is the best way to find the file mime type of remote file in ruby on rails application (eg. I have a file located in s3 and want to check its file type, I don't think checking extension of file is a good idea).
To be specific, I want to find whether the given media is video or audio.
There's a library called ruby-filemagic that can check the content of the file and return the mime type. However, it required to access and read the file and it can be an issue if you need to fetch the content of the file from a remote source.
Please note that in the specific Amazon S3 case, you can also store the mime type of the file to Amazon S£ as object metadata when you upload the file itself. I strongly recommend you to do this, so that you can easily retrieve the metadata and search for the given attribute, instead of guessing it from the file.

Resources