Multiple upload to PHP online server

Asked

Viewed 242 times

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 on LOCALHOST, and need to send the photos to a SERVER. How can I send to the server instead of sending to a localhost?

2 answers

1


Only way to send the image file to another server would be by 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>
  • If the user has FTP access to the server, he could not then send the PHP source to the server, via FTP client, and then do there the same thing he did on localhost, without any additional implementation?

  • 1

    In this case he made it clear that he uses the localhost system and would like to save the images on an external server, this was the problem.

  • @C.Bohok No, the idea is just an external upload, and using localhost, because these are systems installed on several other desktops that need to run on localhost, because they use external components (biometric reader) and cannot be 100% online.

0

FTP nay It’s the only way you can use it, it actually has several. By the way, if you are already going to run in that target PC of the images the FTP server, could run any server. Other ways to implement can be:

  • SFTP - is very similar to normal FTP, but through a secure connection (example and setting of the example).
  • SCP - is a method for secure copying of files between different machines (manual for ssh2_scp_send).
  • PHP - can use an Apache server with PHP to expose a service that receives the file and saves in the correct location, almost the same as you already have to localhost.

Browser other questions tagged

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