Upload multiple to PHP online server

0

I have the following function to send multiple images:

//Diretório onde a imagem será gravada temporariamente
$dirToSave = 'assets/uploads/'.$pasta.'/'.$tipo.'/';
if(!is_dir($dirToSave)){
    echo "noa tem";
    mkdir($dirToSave, 0777, TRUE);
    chmod($dirToSave, 0777);
}

//Limite do tamanho máximo que a imagem deve ter
$length         =   5242880; //5 MB por arquivo

//Extensões permitidas para os arquivos
$fileExtension  =   array( 'jpg', 'jpeg', 'png' );

//Inicializa os parametros necessários para upload da imagem
$this->files->initialize( $dirToSave, $length, $fileExtension );

//Verifica se alguma imagem foi selecionada
$image         =   isset( $_FILES[ $campo ] ) ? $_FILES[ $campo ] : null;



if( !is_null( $image ) ) {

    //Seta o arquivo para upload
    $this->files->setFile( $image );

    //Processa o arquivo e recebe o retorno
    $upload  =   $this->files->processMultFiles($campo);

    //Verifica retornbou algum código, se sim, ocorreu algum erro no upload
    isset( $upload['code'] ) ? 'mensagem de erro' : null;

    foreach($upload as $valor){
        $fotos['int_id'] = $cod;
        $fotos['inf_tipo'] = $tipo;
        $fotos['inf_imagem'] = str_replace('assets/uploads/'.$pasta.'/'.$tipo.'/', "", $valor['file']);

        if(preg_match('%assets/uploads%', $fotos['inf_imagem'])==true){

        } else { 
            $this->adicionar('interno_fotos', $fotos);
        }
    }
}

However, I use this system in LOCALHOST, and I need to send the photos to a SERVER. In what way can I do to send to the server instead of sending to a localhost?

    
asked by anonymous 22.11.2017 / 14:36

2 answers

1

Only way to send the image file to another server would be sending the image via FTP to this server.

Having in hand server address, FTP user and password, follows example:

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
        var_dump( $_FILES );//apenas para debug


        $servidor = 'host';
        $usuario_ftp = 'lalalala';
        $senha = '1234';
        $pasta_do_ftp = '/httpdocs/uploads/';
        $arquivo = $_FILES['arquivo'];

        $con_id = ftp_connect($servidor) or die( 'Não conectou em: '.$servidor );
        ftp_login( $con_id, $usuario_ftp, $senha );

        ftp_put( $con_id, $pasta_do_ftp.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );
}
?>
        <form action="" method="post" enctype="multipart/form-data">
                <input type="file" name="arquivo" />
                <input type="submit" name="enviar" value="Enviar" />
        </form>
    
22.11.2017 / 17:14
0

FTP no is the only way you can use it, it actually has several. Incidentally, if you are already going to run on that destination PC's images FTP server, could run any server. Other ways to implement can be:

  • SFTP - is much like normal FTP, but over a secure connection ( example and fit the example ).
  • SCP - is a method for securely copying files between different machines (manual for ssh2_scp_send ).
  • PHP - You can use an Apache server with PHP to expose a service that receives the file and saves it in the correct location, practically the same as you already have for localhost .
30.11.2017 / 14:52