devSSI's avatar
Level 5

Upload image to the third service

I am trying to upload image to the third service, but I am receiving error

{"success":false,"error":"Could not download the remote file: C:\\wamp64\\tmp\\phpB5D6.tmp"}

So my first guess was that I cannot upload image to the service from my computer (localhost), instead I should put the file into storage and then send. I have tried this approach, too, but seems like it still does not work.

This is how I store images into storage:

$file = $request->file('file');
return Storage::disk('public')->put('/temp', $file);

This is how I tried to provide them to the service:

if($file) {
            $data = ['file' => storage_path(). Storage::url('public/'.$fileStoragePath)];
        } ....

Any ideas what is wrong here? :) Thanks

0 likes
5 replies
Snapey's avatar

Sounds like you need to put the file on a public disk so that it can be collected by the third party.

/temp is not a valid public location. The file should be in storage/app/public and you must have used the storage:link artisan command

But finally, and most importantly, how is the third party able to reach inside your private network and open a file on your wamp server?

devSSI's avatar
Level 5

@Snapey thanks for the response. That directory is also in the public, so it should be visible. I just created it for ease of usage in the backend :)

devSSI's avatar
devSSI
OP
Best Answer
Level 5

For the future references: The issue was, that service was not able to access or receive files from localhost. I deployed this feature and it worked, but after some tweaks.

After upload on my filesystem, I had to send that file like:

['expected_var_name' => url('/').Storage::url('public/'.$pathToMyUploadedFile)];
LaryAI's avatar
Level 58

To upload an image to a third-party service, you need to ensure that the file is accessible and properly formatted for the request. The error message you are seeing suggests that the third-party service cannot access the file from your local path. Here’s a step-by-step solution to address this issue:

  1. Store the file in a publicly accessible location: Ensure that the file is stored in a location that can be accessed by the third-party service. Using Laravel's Storage facade, you can store the file in a public disk.

  2. Retrieve the file URL: After storing the file, retrieve its URL to send it to the third-party service.

  3. Send the file using a multipart/form-data request: Use a library like Guzzle to send the file to the third-party service.

Here’s how you can achieve this:

Step 1: Store the File

Store the file in a publicly accessible location using Laravel's Storage facade.

$file = $request->file('file');
$filePath = $file->store('temp', 'public');

Step 2: Retrieve the File URL

Retrieve the URL of the stored file.

$fileUrl = Storage::url($filePath);

Step 3: Send the File to the Third-Party Service

Use Guzzle to send the file to the third-party service. Ensure you include the file in a multipart/form-data request.

use GuzzleHttp\Client;

$client = new Client();
$response = $client->post('https://third-party-service.com/upload', [
    'multipart' => [
        [
            'name'     => 'file',
            'contents' => fopen(public_path($filePath), 'r'),
            'filename' => basename($filePath)
        ],
    ],
]);

$responseBody = json_decode($response->getBody(), true);

Full Example

Here’s the complete code:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use GuzzleHttp\Client;

public function uploadToThirdPartyService(Request $request)
{
    // Step 1: Store the file
    $file = $request->file('file');
    $filePath = $file->store('temp', 'public');

    // Step 2: Retrieve the file URL
    $fileUrl = Storage::url($filePath);

    // Step 3: Send the file to the third-party service
    $client = new Client();
    $response = $client->post('https://third-party-service.com/upload', [
        'multipart' => [
            [
                'name'     => 'file',
                'contents' => fopen(public_path('storage/' . $filePath), 'r'),
                'filename' => basename($filePath)
            ],
        ],
    ]);

    $responseBody = json_decode($response->getBody(), true);

    return response()->json($responseBody);
}

Explanation

  1. Store the file: The file is stored in the public disk under the temp directory.
  2. Retrieve the file URL: The URL of the stored file is retrieved using Storage::url().
  3. Send the file: Guzzle is used to send a multipart/form-data request to the third-party service. The file is read using fopen() and included in the request.

This approach ensures that the file is accessible to the third-party service and properly formatted for the upload request.

Please or to participate in this conversation.