Can I POST a file automatically?

6

I'm using Cron to run a PHP script. I want to do a load upload test for my server and Cron takes charge of this (the server then sends it to the Amazon).

I was thinking of using file-get-contents to upload files to the server but how do I remove the file information? (filename, type, etc.)

I've seen this function in JavaScript to do a POST and send it automatically. Can I do this in pure PHP?

    
asked by anonymous 30.10.2014 / 19:12

3 answers

1

With the help of the @Math answer I remembered Curl to create POST.

So I started by creating my PHP script ( upload_test.php ) where I POST my file ( myfile_test.zip ) to gravar.php and save the result to teste_results.txt .

upload_test.php:

$user_id = rand( 1, 10 );

$local_file = '/my_dir/myfile_test.zip';

$ch = curl_init();
curl_setopt( $ch, CURLOPT_HEADER, 0                              );
curl_setopt( $ch, CURLOPT_VERBOSE, 0                             );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true                   );
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)" );
curl_setopt( $ch, CURLOPT_POST, true                             );
curl_setopt( $ch, CURLOPT_URL, 'http://localhost/gravar.php'     );

$post_array = array(
    "uploaded_file" => "@" . $local_file,
    "function" => "upload",
    "user_id" => "$user_id",
);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_array );
$response = curl_exec( $ch );

$myfile = fopen( "/my_dir/teste_results.txt", "a+" ) or die( "Unable to open file!" );

$txt = "\n=> AUTO_UPLOAD " . date( 'Y-m-d H:i:s' ) . "\n";
fwrite( $myfile, $txt );

fwrite( $myfile, "Fez upload? = " . $response . "\n" );

fwrite( $myfile, "\n" );

fflush( $myfile );
fclose( $myfile );

Curl Fountain

On the side of gravar.php , so I get the file:

$uploaded = (object) $_FILES['uploaded_file'];

$file_name = $uploaded->name;
$file_tmp  = $uploaded->tmp_name;
$file_type = $uploaded->type;
$file_size = $uploaded->size;

Then just add the line to the Crontab and I can create the lines you want to test my load server.

At the command line:

$ crontab -e

Insert the following line and record: (10 in 10 minutes)

*/10 * * * * /usr/bin/php /var/www/html/my_dir/upload_test.php

Source of crontab

    
03.11.2014 / 16:57
5

You can use the AWS SDK for PHP - ( link )!

There you can control and send files to amazon through php, even if it's a script running in cron.

Now, to read the information from the folder, you can use the opendir function.

<?php
$dir = "/etc/php5/";

// Abre um diretorio conhecido, e faz a leitura de seu conteudo
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>
    
30.10.2014 / 19:16
2

By correctly setting the request header, you can send a file upload either with cURL or file_get_contents .

// post_falso.php

<?php

define('MULTIPART_BOUNDARY', '---'.microtime(true));
define('FORM_FIELD', 'uploaded_file');

$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;


$filename = "c:\teste.txt";
$file_contents = file_get_contents($filename); //carrega o arquivo falso

$content =  "--". MULTIPART_BOUNDARY ."\r\n".
        "Content-Disposition: form-data; name=\"". FORM_FIELD. "\"; filename=\"".basename($filename)."\"\r\n".
        "Content-Type: application/zip\r\n\r\n". //define o mime type

        $file_contents."\r\n";

//adiciona uma campo chamado foo com o valor bar.
$content .= "--".MULTIPART_BOUNDARY."\r\n".
        "Content-Disposition: form-data; name=\"foo\"\r\n\r\n"."bar\r\n";

// fecha o cabeçalho, é obrigatório usar dois traços a mais
$content .= "--".MULTIPART_BOUNDARY."--\r\n";


//Define o cabeçalho http
$context = stream_context_create(array(
        'http' => array(
                'method' => 'POST',
                'header' => $header,
                'content' => $content,
                'user_agent' => '?',
        )
));

$response = file_get_contents('http://teste/gravar.php', false, $context);
echo $response;

gravar.php

<?php
echo '<pre>';
print_r($_POST);
print_r($_FILES);
move_uploaded_file($_FILES['uploaded_file']['tmp_name'],
                  'caminho\nome_do_arquivo'. $_FILES['uploaded_file']['name']);

Refinements:

Upload a file using file_get_contents

How to post data in PHP using file_get_contents?

How does HTTP file upload work?

    
03.11.2014 / 01:23