Upload file to FTP, directly from a URL

0

I tried several ways to do something like "php file reads the file in url, writes another file within ftp"

Code tested 1:

<!-- language: lang-php -->
<?php
    if(empty(  $_POST['q']))
        exit('Use o parametro q');

    $url = 'https://www.youtube.com/watch?v=' . $_POST['q'];

    $ch = curl_init();
    $Variaveis = array(
        'format' => 'json',
        'video' => $url
    );
    $url = 'http://youtubeinmp3.com/fetch/?' . http_build_query($Variaveis);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);

    $dados = json_decode($response, true);

    $src = fopen($dados['link'], 'r');
    $dest = fopen($_POST['f'] . '.mp3', 'w');
    stream_copy_to_stream($src, $dest);
?>

In FTP it copies files with .txt extension .png (Direct Files), when it is the one of the URL that I need, it creates the file, but does not write, and stays with 0kbs.

Now when I testo in localhost here at home it works normal.

    
asked by anonymous 10.11.2015 / 13:34

1 answer

0

Try uploading in steps ... And always close the connection.

while (!feof($src)) 
{ 
    stream_copy_to_stream($src, $dest, 1024);// no caso 1024 bytes por vez. 
}
fclose($src); 
fclose($dest);
    
10.11.2015 / 14:02