How To Download Website Content into File Using PHP Tutorial

Reading Time: 3 minutes
1,106 Views

Inside this article we will see the concept i.e How To Download Website Content into File Using PHP. Article contains classified information about website page content scrapping and download it in PHP. How To get website page content and save into an external file we will see inside this article.

If you are looking for an article which gives you the information about website page scrapping in PHP then this article will give a better solution for this. Also the same concept you can understand it as Downloading website page source code into file in PHP.

Learn More –

Let’s get started.

Application Programming

Create a file called it as index.php inside your project folder. Open index.php and write this code into it.

We will see file writing by these functions of PHP – file_put_contents() & fopen()

Using file_put_contents()

<?php


$ch = curl_init("https://www.tutorialspoint.com/index.htm");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

$content = curl_exec($ch);

curl_close($ch);

$file = "website.txt";
@file_put_contents($file, $content);

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);

Using fopen()

Inside this, we will use fopen() PHP function to write file content and then save.

<?php


$ch = curl_init("https://www.tutorialspoint.com/index.htm");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

$content = curl_exec($ch);

curl_close($ch);

$file = "website.txt";
$txt = fopen($file, "w") or die("Unable to open file!");
fwrite($txt, $content);
fclose($txt);

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);

Application Testing

Now,

Open into browser.

URL: http://localhost/index.php

Once you run your application, page content will be save into website.txt file. File will be downloaded automatically when once you execute this code.

website-page-scrapping-in-php-tutorial

We hope this article helped you to learn How To Download Website Content into File Using PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.