Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

richard nicson

Upload de Imagem por url

Recommended Posts

Boa tarde,gostaria de fazer o upload de criar uma pasta nomeada com o id e depois fazer upload de uma imagem dentro da nova pasta através da url,eu estou pegando a url mas ela não está salvando no servidor a imagem.

 

 

O que tenho pronto

 

$teste = simplexml_load_string(file_get_contents('http://noticias.r7.com/brasil/feed.xml'));




foreach($teste->entry as $ind){
    $newDom = new DOMDocument();
    @$newDom->loadHTML($ind->content);
    
    $pega_tag = $newDom->getElementsByTagName('img');
    
}


if($pega_tag->length > '0'){
        $url_origem = $pega_tag->item(0)->getAttribute('src');


    }


$arquivo_destino = "/home/associac/public_html/";


function recebe_imagem ($url_origem,$arquivo_destino){ 
$minha_curl = curl_init ($url_origem); 
$fs_arquivo = fopen ($arquivo_destino, "w"); 
curl_setopt ($minha_curl, CURLOPT_FILE, $fs_arquivo); 
curl_setopt ($minha_curl, CURLOPT_HEADER, 0); 
curl_exec ($minha_curl); 
curl_close ($minha_curl); 
fclose ($fs_arquivo); 
} 

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Aparentemente, há dois problemas:

 

1. A função recebe_imagem() abre o arquivo e o fecha, sem escrever nada nele. Use fwrite. Faça o cURL retornar o conteúdo, com CURLOPT_RETURNTRANSFER. Mais detalhes aqui

 

2. A função recebe_imagem() nunca é chamada

Compartilhar este post


Link para o post
Compartilhar em outros sites
$teste = simplexml_load_string(file_get_contents('http://noticias.r7.com/brasil/feed.xml'));



foreach($teste->entry as $ind){
    $newDom = new DOMDocument();
    @$newDom->loadHTML($ind->content);
    
    $pega_tag = $newDom->getElementsByTagName('img');
    
}


if($pega_tag->length > '0'){
        $url_origem = $pega_tag->item(0)->getAttribute('src');


    }


$arquivo_destino = "/public_html/";


function recebe_imagem ($url_origem,$arquivo_destino){ 
$minha_curl = curl_init ($url_origem); 
$fs_arquivo = fopen ($arquivo_destino, "w"); 
$fs_arquivo = fwrite($fs_arquivo,$idr7);
curl_setopt ($minha_curl, CURLOPT_FILE, $fs_arquivo); 
curl_setopt ($minha_curl, CURLOPT_HEADER, 0); 
curl_exec ($minha_curl); 
curl_close ($minha_curl); 
fclose ($fs_arquivo); 
}

Acredito que tenha algo errado no código

Compartilhar este post


Link para o post
Compartilhar em outros sites

@Beraldo já apontou os problemas principais. Faça as correções e, após, poste os resultados aqui.

Compartilhar este post


Link para o post
Compartilhar em outros sites

coloquei a função dentro do if

 

$teste = simplexml_load_string(file_get_contents('http://noticias.r7.com/brasil/feed.xml'));




foreach($teste->entry as $ind){
    $newDom = new DOMDocument();
    @$newDom->loadHTML($ind->content);
    
    $pega_tag = $newDom->getElementsByTagName('img');
    
}


if($pega_tag->length > '0'){
        $url_origem = $pega_tag->item(0)->getAttribute('src');


$arquivo_destino = "/public_html/";


function recebe_imagem ($url_origem,$arquivo_destino){ 
$minha_curl = curl_init ($url_origem); 
$fs_arquivo = fopen ($arquivo_destino, "w"); 
$fs_arquivo = fwrite($fs_arquivo,$idr7);
curl_setopt ($minha_curl, CURLOPT_FILE, $fs_arquivo); 
curl_setopt ($minha_curl, CURLOPT_HEADER, 0); 
curl_exec ($minha_curl); 
curl_close ($minha_curl); 
fclose ($fs_arquivo); 
}  
    }


Porém continua não incluindo na pasta fotos

Compartilhar este post


Link para o post
Compartilhar em outros sites

Coloquei esse script.


$teste = simplexml_load_string(file_get_contents('http://noticias.r7.com/brasil/feed.xml'));


foreach($teste->entry as $ind){
    $newDom = new DOMDocument();
    @$newDom->loadHTML($ind->content);
    
    $pega_tag = $newDom->getElementsByTagName('img');
    
}

if($pega_tag->length > '0'){
        $url_origem = $pega_tag->item(0)->getAttribute('src');
	
$arquivo_destino = "/public_html/";
	
function recebe_imagem ($url_origem,$arquivo_destino){ 
$minha_curl = curl_init ($url_origem); 
$fs_arquivo = fopen ($arquivo_destino, "w"); 
$fs_arquivo = fwrite($fs_arquivo,$idr7);
curl_setopt ($minha_curl, CURLOPT_FILE, $fs_arquivo); 
curl_setopt ($minha_curl, CURLOPT_HEADER, 0); 
curl_exec ($minha_curl); 
curl_close ($minha_curl); 

fclose ($fs_arquivo); 
} 	
    }
recebe_imagem();

Me forneceu esses erros

Warning: Missing argument 1 for recebe_imagem(), called in /home/brmaste1/public_html/new.php on line 64 and defined in /home/brmaste1/public_html/new.php on line 52

Warning: Missing argument 2 for recebe_imagem(), called in /home/brmaste1/public_html/new.php on line 64 and defined in /home/brmaste1/public_html/new.php on line 52

Warning: fopen() [function.fopen]: Filename cannot be empty in /home/brmaste1/public_html/new.php on line 54

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/brmaste1/public_html/new.php on line 56

Warning: fclose(): supplied argument is not a valid stream resource in /home/brmaste1/public_html/new.php on line 61

Compartilhar este post


Link para o post
Compartilhar em outros sites

Você deve passar os argumentos que você criou.

Não basta criar variáveis com os mesmos nomes dos parâmetros e chamar a função sem parâmetro algum.

Você deve sempre passar parâmetros quando a função assim exige:

 

 

$url_origem = $pega_tag->item(0)->getAttribute('src');
$arquivo_destino = "/public_html/";
 
// ...
 
recebe_imagem($url_origem, $arquivo_destino);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Faça debug do seu código

Habilite todas as exibições de erro, como mostrado neste tópico, na Orientação No. 3

 

Só dizer que "não funciona" não adianta. Tem que descrever o problema, mandar as mensagens de erro...

Compartilhar este post


Link para o post
Compartilhar em outros sites

Warning: Missing argument 1 for recebe_imagem(), called in /home/brmaste1/public_html/new.php on line 64 and defined in /home/public_html/new.php on line 52

Warning: Missing argument 2 for recebe_imagem(), called in /home/brmaste1/public_html/new.php on line 64 and defined in /home/public_html/new.php on line 52

Warning: fopen() [function.fopen]: Filename cannot be empty in /home/public_html/new.php on line 54

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/public_html/new.php on line 56

Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/new.php on line 61

Compartilhar este post


Link para o post
Compartilhar em outros sites

O erro explica o problema:

 

Missing argument 1 for recebe_imagem() = Falta o argumento 1 da função recebe_imagem()

Missing argument 2 for recebe_imagem() = Falta o argumento 2 da função recebe_imagem()

 

Você chamou a função sem passar os argumentos.

Expliquei isso no Post #10 deste tópico

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.