-1

I have this code but it's not working, can someone tell me what to do? I don't know PHP, just started to learn PHP. I'm trying to put url in form and get that file downloaded from url to my server.

        $prevod = $_POST['prevod'];
        $url = file_get_contents("$prevod");
        $fp = fopen("prevodi/", "w");
        fwrite($fp, $url);
        fclose($fp);

        <form action="prevod.php" method="post"> 
        <input name="prevod" type="text"/> 
        <input type="submit" value="Pronađi"/>
        </form>
  • for downloading file you can use `curl`. – mortymacs Jun 18 '14 at 06:01
  • If you want to download file from ANOTHER server use CURL, if You trying to download file from your form create an input with type="file" ;) Then receive it in $_FILE variable – Mr.TK Jun 18 '14 at 06:01
  • possible duplicate of [Download files from url to server](http://stackoverflow.com/questions/24282169/download-files-from-url-to-server) – rvignacio Jul 07 '14 at 16:56

2 Answers2

0

Try this and please avoid quotes for variables

$prevod = $_POST['prevod']; $url = file($prevod);

you have to give path before filename then it will work if it was protected then you have to access by curl

    $urldata = realpath('../severname/folder/'.$prevod.'');
    $fp = fopen('../foldername/subfolder','w');
    $newfile = realpath('../foldername/subfolder/'. $prevod .'');
    file_put_contents($newfile, $urldata);

    <form action="prevod.php" method="post"> 
    <input name="prevod" type="text"/> 
    <input type="submit" value="Pronađi"/>
    </form>
Muthukrishna C
  • 147
  • 1
  • 13
  • I need to download file from another server to my server. If it's easier for you. I have variable which gets that URL (file) $HasPrevod, that's the file I need to download to my server. It's on another server. It's defined by $HasPrevod. So I need to download that file (defined by $HasPrevod variable) and save it to my server in folder prevodi. – user3739435 Jun 18 '14 at 06:57
0

Create prevod.php then add following code. Please create "prevodi" directory also. You can change file name by variable $with_extension. Now it is for gif file.

 <?php $ch = curl_init();
    $source =  $_POST['prevod'];
    curl_setopt($ch, CURLOPT_URL, $source);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec ($ch);
    curl_close ($ch);

    $with_extension="filename.gif";
    $destination = "prevodi/". $with_extension;
    $file = fopen($destination, "w+");
    fputs($file, $data);
    fclose($file); 
 ?>
Arshid KV
  • 9,631
  • 3
  • 35
  • 36