Send file via cURL from form POST in PHP

Send file via cURL from form POST in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Send file via cURL from form POST in PHP

Handling file uploads on a standard HTTP request is straightforward when using $_FILES and moving data to permanent storage. However, scenarios requiring server-to-server communication or asynchronous processing often demand sending that same multipart data via cURL. The confusion usually stems from how the client-side upload differs from the server-side re-upload.

When a user submits a form with <form action="script.php" method="post" enctype="multipart/form-data">, the browser constructs a complex MIME boundary string and transmits the file content directly to script.php. In that specific context, $_FILES['image']['tmp_name'] refers to a temporary path created by PHP on the local filesystem (e.g., /tmp/phpXXXXXX).

The challenge arises when you need to forward this file from script.php to another endpoint using cURL. You cannot simply pass the original filename string or the raw file content as a standard POST variable. The receiving server expects an HTTP multipart request, not a plain text field containing a path.

To replicate the client's behavior in your cURL call, you must construct the multipart payload manually. This involves setting CURLOPT_POSTFIELDS to a specific string format that includes boundaries and file data. However, PHP provides a helper for this exact scenario: curl_file.

Instead of constructing the multipart string manually, which is error-prone and difficult to maintain, use curl_file(). This function accepts the local path to the file (derived from $_FILES['image']['tmp_name']), the original filename ($_FILES['image']['name']), and an optional MIME type. It automatically handles the boundary generation and headers required for a multipart POST.

Here is how you would implement this in script.php:

if (isset($_POST['upload'])) {
    // Validate that a file was actually uploaded
    if (!empty($_FILES['image']['tmp_name'])) {
        $file = $_FILES['image'];
        
        // Construct the cURL resource for the file
        $curlFile = [
            'image' => curl_file::create($file['tmp_name'], $file['name'])
        ];

        $ch = curl_init('https://destination-endpoint.com/upload');

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlFile);
        
        // Optional: Set Content-Type to multipart/form-data automatically via cURL_FILE
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        curl_close($ch);
    }
}

The receiving script, curl_receiver.php, must be configured to handle multipart data. Unlike standard POST requests where you might use $_POST to access form fields, file uploads populate the $_FILES superglobal array. The receiver should validate the incoming request to ensure it is indeed a multipart upload and not just a text string labeled 'image'.

// curl_receiver.php
if (!empty($_FILES['image'])) {
    $tmpName = $_FILES['image']['tmp_name'];
    $filename = $_FILES['image']['name'];
    $size = filesize($tmpName);
    
    // Process the file (e.g., move to permanent storage)
    if (move_uploaded_file($tmpName, '/storage/' . $filename)) {
        echo "File uploaded successfully.";
    } else {
        echo "Failed to move file.";
    }
} else {
    echo "No file was received in the POST request.";
}

A critical pitfall to avoid is attempting to pass the filename as a simple string value in the $post array, such as 'image' => @path/to/file. This syntax works only for local file uploads initiated by a browser form where the @ tells cURL to read from the path. When you are programmatically sending data via cURL on a different server, the receiving endpoint expects the binary data and headers of a multipart message, not a reference to a file on a remote machine's filesystem (unless that remote machine is the one serving the file).

Furthermore, security considerations are paramount when handling $_FILES['image']['name']. Attackers often submit malicious filenames like ../../etc/passwd or executable scripts. While modern PHP configurations restrict access to temporary files, it is best practice to sanitize filenames before moving them to a permanent directory, stripping extensions or enforcing a whitelist of allowed characters if the application logic permits it.

For projects built on the Laravel framework, handling this flow is even more streamlined because Laravel provides a dedicated Storage facade that abstracts away the file system details and handles temporary file processing securely.

Our network