How to upload using php without forms

Asked

Viewed 539 times

2

I need to upload a file from the local server to another server. In this environment I compact the file on the local server and send the compressed file to the remote server. The remote server will have the validation specifications rules to know whether to accept the file or not and etc... Would it be possible for me to send a file that is in /tmp/test.tar.gz to the remote server? if yes, as I would send the file on that request (on the local server) and receive on the other side (on the remote server)?

the use of file_get_content, in this case I think it would be impracticable because the file could be large, and I believe that when it was loaded into the server memory would generate a high consumption of resources.

The idea would be to have something simple like http.

I started trying to use the CURL, but in the case of Curl I’m having trouble getting the remote server to recognize the parameters I’m sending by get.

Class System{
    public static function getContentURI($uri, array $post = array(), $header = array())     {
        if (empty($uri)) {
            return false;
        }
        //Initialise the cURL var
        $ch = curl_init($uri);

        //Get the response from cURL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        if (!empty($post)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        // Execute the request
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            throw new \Exception($response . ': ' . curl_error($ch));
        }
        curl_close($ch);
        return $response;
    }
    public static function parseFileToParamContentURI($file, $field) {
        if (!file_exists($file)) {
            return false;
        }

        // Recupera o nome do arquivo
        $filename = pathinfo($file, PATHINFO_FILENAME);
        // Recupera o mime-type
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime = finfo_file($finfo, $file);
        finfo_close($finfo);

        //Create a POST array with the file in it
        $dataFile = array(
            $field => '@' . $file
            . ';filename=' . $filename
            . ';type=' . $mime,
        );

        return $dataFile;
    }
}

The code I’m using to send is:

// Servidor que deveria receber o upload
$uri = 'http://127.0.0.1' . $this->get('router')->generate('receive_log') . '?' . http_build_query(array('chave' => <teste>));
// Formata o arquivo no formato que possa ser enviado
$params = System::parseFileToParamContentURI('/tmp/test', 'file');
// Requisição CURL
$resp = System::getContentURI($uri, $params);

With Curl I am not able to receive the parameters I pass by get on the server, the functions that used Curl I switched to the command redirect symfony.

1 answer

1


I found reading the file unnecessary, here a simple example. of a set in your code, this should work.

<?php
if(!empty($_FILES) || !empty($_POST)){
    var_dump($_FILES, $_POST);
    die;
}


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/test/curl.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => "@".realpath('curl.php'), //arquivo, o @ le e envia o arquivo
        'input_qualquer' => 'olaaaa'
));
$result = curl_exec($ch);
curl_close($ch);

var_dump($result);

but in your case the problem must be:

missed

curl_setopt($ch, CURLOPT_POST, true);

and adjust the array

        //Create a POST array with the file in it
    $dataFile = array(
        'field' => '@' . realpath($file),
        'filename' => $filename,
        'type' => $mime,
    );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.