Upload file to FTP, directly from a URL

Asked

Viewed 66 times

0

I tried a lot of ways to do something like "php file reads the URL file, write another same one inside 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 extension . txt . png (Direct files), when it is the URL I need, it creates the file, but does not write, and gets 0kbs.

Now when testo on localhost here at home works normal.

  • 1

    Add the code and then press { } formatting.

1 answer

0

Try uploading in stages... 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);
  • Did not work on FTP, on localhost works of both methods

  • Does it give an error? Maybe some limit on FTP configuration.

  • No, it just doesn’t write the file to FTP, however when I do the same process with a file of any size(tried 200mb) it was direct link, it worked perfectly

Browser other questions tagged

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