Upload to another server error

Asked

Viewed 1,147 times

2

I am trying to record some images on another server pointing the IP and the folder, in my PHP code I pointed the path of the folder $_UP['pasta'] = '\\\\172.23.25.4\\\manutencao\\';and the following error occurs below:

Warning: move_uploaded_file( 172.23.25.4 manutencao 1415978389.jpg): failed to open stream: Permission denied in /home/Sistemas/celu/public_html/souza/MANUTENCAO/projetos/AD-ipco-Cod-01/fontes/cadastro/cadastro.php on line 126 Warning: move_uploaded_file(): Unable to move '/tmp/phpEQFSvN' to ' 172.23.25.4 maintenance 1415978389.jpg' ... on line 126 Could not upload file, please try again

If I point to the path of the local folder, it works normal, but if I point the path of the folder to another server this error occurs.

I have granted folder and user permissions as per my search.


I created a new test file and now error occurs in the ftp_put() function follows the error

CONNECTED TO THE SERVER 172.23.25.4, WITH THE USER TEST Warning: ftp_put(): Could not create file. in /home/Sistemasinternos/cel/public_html/jbarbin/upload/teste2/ftp_envia.php on line 28 FTP upload has failed!

follows the code

<form action="ftp_envia.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>

Below the php code

<?php
ftp_server = "172.23.25.4";
$ftp_user_name = "teste";
$ftp_user_pass = "123456";
$destination_file = "/home/teste";
$source_file = $_FILES['file']['tmp_name'];

$conn_id = ftp_connect($ftp_server);

ftp_pasv($conn_id, true); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name <br>"; 
    exit; 
} else {
    echo "CONETADO NO SERVIDOR $ftp_server, COM O USUARIO $ftp_user_name";
}

ftp_pasv($conn_id, true); 

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file,  FTP_ASCII); 

// check upload status
if (!$upload) { 
    echo "<br>FTP upload has failed!";
} else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream 
ftp_close($conn_id);
?>
  • Check if you have permission to this folder via code, if not, give access to it.

  • have already given access to folder and same error occurs

  • you can access this directory via explorer or browser?

  • I can access normal via explorer and browser ..

  • By chance, this remote address that I assume can be successfully accessed via network, is a Windows PC? I ask because I had the same problem with the VM in which I use Linux Mint (based on Ubuntu) and while I didn’t edit the fstab by mounting the path of this shared folder (which in my case is an entire partition) I couldn’t even create a new file, let alone upload one.

3 answers

2

You can’t use it move_uploaded_file to upload a file to a remote server.
The easiest way to achieve this is by using PHP’s native FTP functions.

$ftp_server = "ftp.example.com";
$ftp_user = "o_seu_username";
$ftp_pass = "a_sua_password";

$file = "path/para/o/ficheiro.txt";
$remote_file = "ficheiro.txt";

// Criar ligação
$conn_id = ftp_connect($ftp_server) or die("Não conseguiu conectar com $ftp_server"); 

// Ir para a pasta onde quer carregar o ficheiro
ftp_chdir($conn_id, '/www/site/caminho/para/a/pasta');


// Efectuar login no FTP
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    // Carregar ficheiro para servidor remoto
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
       echo "Carregado com sucesso de $file\n";
    } else {
       echo "Ocorreu um problema no carregamento de $file\n";
    }
} else {
    echo "Não foi possível conectar como $ftp_user\n";
}

// Fechar conexão
ftp_close($conn_id);

Note: This code has not been tested

If an error occurs, it is possible that the remote server has some kind of firewall and for that use the following code:

// Ligar modo passivo
ftp_pasv($conn_id, true);
  • Thank you, I’ll post my code here for you to see .

1


Solved the problem, I used the following code below: reference:http://wbruno.com.br/php/upload-de-arquivo-ftp-php/

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

    $servidor = 'x.x.x.x';
    $caminho_absoluto = '/home/teste/';
    $arquivo = $_FILES['arquivo'];

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


    $enviado = ftp_put( $con_id, $caminho_absoluto.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );

    if ($enviado){
        echo "Arquivo Enviando com sucesso para o Servidor: ". $servidor . "<br>Caminho do arquivo FTP ". $caminho_absoluto;
    }
}

Form:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="arquivo" />
    <input type="submit" name="enviar" value="Enviar" />
</form>

0

move_upload_file only serves in case of use of an "uploaded file". in your case: 1-check the right of Acceso 2- Use copy()

Example (php.net):

        <?php
           $file = 'example.txt';
           $newfile = 'example.txt.bak';

          if (!copy($file, $newfile))
            {
                echo "falha ao copiar $file...\n";
           }
        ?>

$file can be a URL and $newfile tamben. Caution: copy() will not create folder!

Browser other questions tagged

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