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

Link to comment
Share on other sites

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

Edited by Tom8001
Link to comment
Share on other sites

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);

}

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.