How to upload using php without forms

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 rules validation specifications 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? (on the local server) and receive from the other side (on the remote server)?

The use of file_get_content, in this case I think it would be impractical because the file could be large, and I believe that being loaded into the server's memory would end up generating a high consumption of resources.

The idea would be to have something simple like http.

I started by trying to use CURL, but in the case of curl I'm having trouble the remote server will recognize the parameters that 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 am using to submit 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 the curl I am not getting the parameters I get by getting on the server, the functions that used the curl I changed by Symfony redirect

    
asked by anonymous 16.02.2015 / 17:26

1 answer

1

I found the file reading unnecessary, here a simple example. of a fit 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 should be in:

missing

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,
    );
    
17.02.2015 / 12:29