Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Read content of BLOB file

Tags:

php

blob

I uploaded a *.mp4 video and converted it to a Blob type using JavaScript's new Blob() function. I send this Blob to PHP, using AJAX.

Now I want to read the content of this Blob inside PHP. In other words, I need the binary data of this Blob and want to store it into a PHP variabele.

However, it seems impossible to read a Blob file with PHP, since fread, fopen and file_get_contents are failing all! When I'm opening the Blob URL in my browser the video is playing fine.

My question is, how do I get the binary data of this Blob with PHP, without installing extensions/libraries?

var_dump($_FILES['video']);
Array
(
    [name] => blob
    [type] => video/mp4
    [tmp_name] => C:\xampp\tmp\php43B8.tmp
    [size] => 58
    [error] => 0
)

// Try 1
if ($stream = fopen($_FILES['video']['tmp_name'], 'r')) {

    echo stream_get_contents($stream, filesize($_FILES['video']['tmp_name']));
    fclose($stream);
}

// Try 2
if ($fp = fopen($_FILES['video']['tmp_name'], 'r')) {
    echo fread($fp, filesize($_FILES['video']['tmp_name']));
    fclose($fp);
}

// Try 3
file_get_contents($_FILES['video']['tmp_name'])

Result is always: blob:http://localhost/d53e40bd-686b-46c8-9e81-94789351466f

like image 579
Heiko Avatar asked Oct 24 '25 14:10

Heiko


1 Answers

I know this is a late reply and you may have fixed this issue already but heres what I did for my BLOB processing. Since the blob is already uploaded to the TEMP file location, we first move the uploaded file somewhere using PHP's move_uploaded_file function. From there, you can read contents and such and then delete the file from your server when processing is completed.

Heres a very basic exampleusing an image file:

// Ajax data for posting the blob, remember to set process data to false
formData.append('code', 'uploadedImage');
formData.append('image', blob);
var url = "script.upload.php";
$.ajax({
    type: 'POST',
    url: url,
    data: formData,
    processData: false,
    contentType: false,
    success: function (data) {}
});


// PHP Code for uploading the file to the server
if ($_POST['code'] == "uploadedImage") {
    $data = $_FILES['image']['tmp_name'];
    move_uploaded_file($data, $_SERVER['DOCUMENT_ROOT'] . "/img/" . time() . ".png");
}

We now have a file like 1542839470.png at our base directory / images location. From here, the image can now be read, moved, streamed, stored, whatever. I am using a handy plugin called Croppie to resize, rotate and crop my images as well for anyone looking for a neat tool. By processing them first, I can avoid having to do anything with them after. As you are doing video, this won't apply in your specific case, but it's a handy plugin for people doing similar stuff (like me).

Once you are done with the file, either by curl to YouTube API or whatever processing you do with the finished file, you can simply delete the file using the following. Cache the video or file name and location from above (simple to do by setting a location $var) and feed that into this command.

unlink("path_to_file_location"); //delete it
like image 74
Kaboom Avatar answered Oct 26 '25 03:10

Kaboom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!