Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

php - Download files from url to server

I need PHP code to download file from one server to another. It's defined by variable $HasPrevod. That's the file which I need to download. Also there's simple form where I put link and the output is that variable $HasPrevod, so I had to put it in a href to get link easily to download.

<a href="$HasPrevod">Download</a>. and download it manually and upload via Filezilla on my server. Is there solution which will download content from that variable and put it on my server in some folder. I tried with cURL but nothing happened because I'm newbie and I've made mistake for shure, and I don't know much about PHP and cURL.

So it should be something like this $HasPrevod file download -> my server / folder, and echo file name.

$prevod = $_POST['prevod'];
$url = file_get_contents("$prevod");
$PrevodLink = preg_match('!http://[a-z0-9S -\_.[][]/]+.(?:srt)!Ui', $url, $match1);
$HasPrevod = $match1['0'];

<form action="prevod.php" method="post"> 
<input name="prevod" type="text"/> 
<input type="submit" value="Search"/>
</form>

Thats the code I'm using to put link and get path to the file which is located on another website not my own, then I have to download manually and upload on my server via Filezilla.

<a href= "<?php echo $HasPrevod; ?>">Download</a>




curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "prevodi/". $HasPrevod;
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
file_put_contents('path/to/file.ext', file_get_contents($HasPrevod));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...