Page 1 of 1

Wie lade ich eine Datei aus einer Zeichenfolge hoch, ohne sie auf Festplatte mit Symfony HTTP -Client zu speichern?

Posted: 14 Feb 2025, 12:52
by Anonymous
Ich versuche, eine Datei in eine API hochzuladen, für die ein mehrteiliges Formular hochgeladen werden muss. Bucket mit Flysystem)) Aber ich kann es zum Laufen bringen (erhalte immer einen 422 -Fehler).

Code: Select all

// $filecontent is data from Flysystem
$fileContent = $this->documentStorage->read('remote filename.pdf');

file_put_contents(__DIR__.'/test.pdf', $fileContent);
$fileHandle = fopen(__DIR__.'/test.pdf', 'rb');

$params = [
'body' => [
'bestand' => $fileHandle
],
];

$response = $this->createClient()->request('POST', $url, $params);
Ich habe versucht, den Lokal speichern mit einem Php: // Speicher und php: // temp Stream, aber das funktioniert nicht:

Code: Select all

$stream = fopen('php://temp', 'rb+');
fwrite($stream, $fileContent);
rewind($stream);

$params = [
'body' => [
'bestand' => $stream
],
];

$response = $this->createClient()->request('POST', $url, $params);
< /code>
versucht manuell ein mehrteiliges Formular zu erstellen, funktioniert auch nicht: < /p>
$dp = new DataPart($fileContent);
$formData = new FormDataPart(['bestand' => $dp]);
$preparedHeaders = $formData->getPreparedHeaders();

$params = [
'body' => $formData->bodyToString(),
'headers' => [
trim($preparedHeaders->toString())
],
];

$response = $this->createClient()->request('POST', $url, $params);