Jump to content

PHP Making an archive?


Tom8001

Recommended Posts

I'm new to learning curl and have a few errors, this is my code

<?php

if($_SERVER['REQUEST_METHOD'] == "POST") 
{
    
    $url = $_POST['url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $data = curl_exec($ch);
    curl_close($ch);
    $destination = "archive";
    $file = fopen($destination, "w+");
    fputs($file, $data);
    fclose($file);

}

?>

I'm trying to get the script to download a web page from a URL that has been entered in the HTML form.

 

I'm getting these errors:

 

Warning: fopen(archive): failed to open stream: Permission denied in C:\xampp\htdocs\defaces\index.php on line 14

Warning: fputs() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\defaces\index.php on line 15
 

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\defaces\index.php on line 16

I got it to work fine, but i want to save the file with the url name in it and i am using the $url variable to do it and i get these errors,

 

here is my code

<?php

if($_SERVER['REQUEST_METHOD'] == "POST") 
{
    
    $url = $_POST['url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $data = curl_exec($ch);
    curl_close($ch);
    $destination = "archive/".$url.""; // The error is coming from here, i put it as archive/source_1.html and it saved fine into the directory, but this gives me an error Warning: fopen(archive/http://example.com): failed to open stream: Invalid argument in C:\xampp\htdocs\defaces\index.php on line 14
    $file = fopen($destination, "w+");
    fputs($file, $data, strlen($data));
    fclose($file);

}

?>

Errors:

Warning: fopen(archive/http://example.com): failed to open stream: Invalid argument in  C:\xampp\htdocs\defaces\index.php on line 14

Warning: fputs() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\defaces\index.php on line 15

Warning: fclose() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\defaces\index.php on line 16

nvm i fixed it

if($_SERVER['REQUEST_METHOD'] == "POST") 
{
    
    $url = $_POST['url'];
    $rand = rand();
    
    $url = preg_replace('#^https?://#', '', $url);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    $data = curl_exec($ch);
    curl_close($ch);
    $destination = "archive/$rand-$url.html";
    $file = fopen($destination, "w+");
    fputs($file, $data, strlen($data));
    fclose($file);

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.