0

My problem tried to create a small script that allows me to create a folder and within this a file (jpg) in the Firebase Storage. I found on the web that just placing the name of the folder before the name of the file should be generated, but the file is not generated and loaded. The file is only loaded when I manually create the folder in Storage.

My upload code:

public function upload(){
        $this->bucket->upload(
            file_get_contents($_FILES['uploadedfile']['tmp_name']),
            [
                'name' => "ena/" . $_FILES['uploadedfile']['name']
            ]
        );
}

I am using the Kreait library. Thank you and I look forward to your support.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DesignAP
  • 31
  • 5
  • I tested this locally (with an already present file, not with $_FILES) and it worked for me. Could you debug this by debugging the contents of `file_get_contents($_FILES['uploadedfile']['tmp_name'])` and of `$_FILES['uploadedfile']['name']` to make sure that a) there are file contents and b) that the target path/file name are as you expect? I would also recommend using `move_uploaded_file()` (https://www.php.net/manual/en/function.move-uploaded-file.php) first, and then using `$this->bucket->upload()` with the moved uploaded file. – jeromegamez Sep 03 '19 at 12:43
  • friend thanks for your support. But do you automatically generate the folder? – DesignAP Sep 03 '19 at 14:54
  • Yes, it does for me, that's why I was suggesting the debugging steps and using `move_uploaded_files()`, because that's the difference in my local test script. – jeromegamez Sep 03 '19 at 15:14
  • ok I'm going to try, but if you can load that fraction of code to guide me. I would appreciate. – DesignAP Sep 03 '19 at 16:31

1 Answers1

1

Thanks jeromegamez, the code that implements and works correctly for me. Whose logic is simple, it receives the file and moves it to the server and once moved it loads it to firebase storage.

$target_path = "uploads/"; 

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

    $name =  $_FILES['uploadedfile']['name'];

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 

        $myfile = fopen($target_path, "r")  ;

        echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded"; 

        $this->bucket->upload($myfile, [
            'name' => $_POST["proyecto"] . "/" . $name
        ]);

    } else{ 
        echo "There was an error uploading the file, please try again!"; 
    }
DesignAP
  • 31
  • 5