0

What I did so far:

Okay, so i create a blob file (wav content) and it is stored under blob:http://localhost/cf6fefdc-352e-4cec-aef8-03af6d0d0ef6.

When i put this URL into my browser, it plays the file.

What i need to do I want to convert the blob into an actual file, that i want to store on my webserver. I'm handing the blob object over to a php file via AJAX

$.ajax ({
 type: "POST",
 url:"path/to/my/file.php",
 data: {thefile : blob} ,
     success: function(text) {
         console.log(text);
     },
     error:function(){
         console.log("Error")
     }
});

That works. If I print_r the variable it returns [object Blob]
My PHP

<?php
  $file = $_POST['thefile']; 
  print_r($file);

Can anyone tell me how I can convert and save the file to my server from there on?

Adam
  • 11
  • 1
  • 3
  • Possible duplicate of [How can javascript upload a blob?](https://stackoverflow.com/questions/13333378/how-can-javascript-upload-a-blob) – M. Prokhorov Mar 20 '18 at 17:35

1 Answers1

0

In your file.php add this code

$filePath = 'uploads/' . $_POST['thefile'];


$tempName = $_FILES['thefile']['tmp_name'];


    if (!move_uploaded_file($tempName, $filePath)) {

    echo 'Problem saving file: '.$tempName;
    die();
}

// success report
echo 'success';
luxshan
  • 54
  • 6
  • you need to have "uploads" folder in your root directory – luxshan Mar 20 '18 at 17:03
  • Thank you for your answer. I created the folder (permission 777 just in case) but i get an error saying "Notice: Undefined index: thefile in /Applications/XAMPP/xamppfiles/htdocs/website/wp-content/themes/theme/file.php on line 6
    Problem saving file: " On line 6 it says: $tempName = $_FILES['thefile']['tmp_name'];
    – Adam Mar 20 '18 at 18:03
  • file_put_contents($path, $data); get that blobdata from ajax and use this method to save into your directory – luxshan Mar 20 '18 at 19:13
  • okay, so thats what my php file looks like right now: ` $file = $_POST['thefile']; file_put_contents("test.wmv", $file);` what it does is, it creates a text.wmv file, but it only has 13 byte, if I open it in a text editor, the content is "[object Blob]". so file is created, but still not the right way... any ideas? – Adam Mar 20 '18 at 20:41