Upload Image to Hostinger directory

Asked

Viewed 270 times

2

Guys I’m trying to save (upload) an image in a directory of Hostinger, but I can’t do it.

Below follows me PHP script.

Obs.: The image in Base64 is coming from an Android app.

<?php

$ftp_server = "ftp.endereço.server";
$ftp_user = "*****";
$ftp_pass = "*****";
$imageCodificada = $_POST['imageCodificada'];


//DECODIFICAR IMAGEM
$imageDecodificada = base64_decode($imageCodificada);


// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Não foi possível estabelecer conexão com $ftp_server"); 

// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Conectado! na $ftp_user@$ftp_server\n";
    file_put_contents(include($_SERVER['DOCUMENT_ROOT']."/public_html/imagens/fotos_func/TESTE.jpg"), imageDecodificada);



} else {
    echo "Não foi possível estabelecer conexão com $ftp_user\n";
}

// close the connection
ftp_close($conn_id);  
?>

My PHP Script does not give error, but the file does not appear in the directory.

Could someone help me in how to do this.

Thank you.

  • You have permissions to create a file there?

  • Jorge, thanks for the return. I created a Permit for this. If I did it correctly I have access yes, so much so that in my script echo comes out with "Connected!"... the problem is that the file does not write.

1 answer

1

That "include" doesn’t seem to make sense:

file_put_contents(include($_SERVER['DOCUMENT_ROOT']."/public_html/imagens/fotos_func/TESTE.jpg"), imageDecodificada);

Remove:

file_put_contents($_SERVER['DOCUMENT_ROOT']."/public_html/imagens/fotos_func/TESTE.jpg", imageDecodificada);

Still, you need to upload it via FTP. Because the physical file was created locally. Now you need to upload it. But in your code there is no sending command. You are just opening and closing FTP without doing anything.

  • Daniel, thank you so much for coming back. But you could give me an example of how to do this...is that I’m new to php dev and I don’t know how to do this FTP upload. I’m breaking my head but I can’t send the image to a Hostinger directory.

Browser other questions tagged

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