Upload to other domain

Upload to other domain

itarodrigoitarodrigo Posts: 15Questions: 6Answers: 0
edited April 2021 in Editor

Field::inst('tb_usuario.imagem')
->setFormatter('Format::ifEmpty', null)
->upload(Upload::inst('http://MY_OTHER_SITE.COM/image/__ID__.__EXTN__')
->db('tb_imagem', 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE,
'web_path' => Upload::DB_WEB_PATH,
'system_path' => Upload::DB_SYSTEM_PATH
))
->validator(function ( $file ) {
return $file['size'] >= 2000000 ?
"Imagens devem ser menor que 2MB" :
null;
} )

How to upload a image to other site? I have 3 sites and I want store the images in a unique local.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You'd use a custom upload action. Instead of a simple move_uploaded_file (unless you've got a network mount of your other site I suppose) you'd need to use an API to upload the file to the other site - S3 for example, or FTP.

    Allan

  • itarodrigoitarodrigo Posts: 15Questions: 6Answers: 0
    edited August 2021

    I am trying:

    Field::inst('importa_cnes.lfces004')
            ->validator(Validate::required(ValidateOptions::inst()
                ->message('Campo obrigatório!')
            ))
            ->upload(
                Upload::inst(function($file, $id){
                    $remote_file = "$id.txt";
    
                    $conn_id = ftp_connect('ftp.******.com.br');
                    $login_result = ftp_login($conn_id, '*****', '*****');
    
                    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)){
                        echo "successfully uploaded $file\n";
                    } else {
                        echo "There was a problem while uploading $file\n";
                    }
    
                    ftp_close($conn_id);
                    move_uploaded_file($file['tmp_name'], '/arquivos/' . $id);
                    return $id;
                })
                ->db('arquivos', 'id', array(
                     'fileName' => Upload::DB_FILE_NAME,
                     'fileSize' => Upload::DB_FILE_SIZE,
                     'web_path'    => Upload::DB_WEB_PATH,
                     'system_path' => Upload::DB_SYSTEM_PATH
                ))
            )
            ->setFormatter('Format::nullEmpty')
    

    But I receive the error: Cannot set path information in database if a custom method is used to save the file.

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The error is correct - if you are using a custom function to manipulate the uploaded file (in this case sent it to an ftp server) we can't know what the web-path will be. You need to remove:

                     'web_path'    => Upload::DB_WEB_PATH,
                     'system_path' => Upload::DB_SYSTEM_PATH
    

    Allan

  • itarodrigoitarodrigo Posts: 15Questions: 6Answers: 0

    Thanks Allan, one more question: the function ftp_connect() is not recognized, why?

    How to call a PHP function inside the Field code?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    If you have ftp support for PHP compiled in, then it should be a global function. If PHP is saying it isn’t a known function, then it suggests to me that ftp support hasn’t been included in however that PHP install was compiled.

    Allan

Sign In or Register to comment.