Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:49:59+00:00 2026-05-26T06:49:59+00:00

I am using CURL to download 4 files from the url for which i

  • 0

I am using CURL to download 4 files from the url for which i have to authenticate myself. and i am authenticating myself with CURL only. But the problem is when i download data it only download 1KB of each file. I tried various methods given on SO. But none of them are working for me. My CURL-PHP code is

<?php

  require ('dbconfig/dbconfig.php'); //database connection.
  ini_set('display_errors', 1); 
  error_reporting(E_ALL);
  $username = "user";
  $password = "password";
  $url = "http://domain-name/contents/";

  global $ch;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
  curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
  $output = curl_exec($ch);
  $r = time()-(24*60*60); 
  $dateit = date("Ymd", $r);

  $file1 = "aaa".$dateit.".tbz";
  $file2 = "aaa".$dateit.".tbz.md5";
  $file3 = "bbb".$dateit.".tbz";
  $file4 = "bbb".$dateit.".tbz.md5";

  $arr = array($file1, $file2, $file3, $file4);

  for($i = 0; $i <= 3; $i++) {

    $url = "http://domain-name/content/current/".$arr[$i];

    $writefn = function($ch, $chunk) { 
      static $data='';
      static $limit = 500; // 500 bytes, it's only a test

      $len = strlen($data) + strlen($chunk);
      if ($len >= $limit ) {
        $data .= substr($chunk, 0, $limit-strlen($data));
        echo strlen($data) , ' ', $data;
        return -1;
      }
      $data .= $chunk;
      return strlen($chunk);
    };

    $ch = curl_init();
    $fp = fopen("adminArea/folder/".$arr[$i], 'w+');

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RANGE, '0-500');
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    curl_exec ($ch);

  }

  fclose($fp);
  curl_close($ch);

?>

Firstly i am authenticating myself then in for loop for all four i am using curl to download data. I got this curl code too from SO. Tried a lot of ways to do the same.

Any help or idea will be highly appreciated.

UPDATE

code worked for me. I added curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); both times as firstly it will authenticate and return response and then download file for me from other url.

 // Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

// Do it
curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL, $urlbase.$file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen("adminArea/folder/$file", 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);

// Do it
curl_exec ($ch);

// Close pointers    
fclose($fp);
curl_close($ch);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:49:59+00:00Added an answer on May 26, 2026 at 6:49 am

    I think the problem is most likely to do with the callback write function. I can;t quite work out exactly what this was supposed to achieve as it is, but it certainly isn’t doing you any favours. Indeed, it seems to me like there’s a lot of code there that is not required for what you describe.

    Try this:

    <?php
    
      // Database connection - do you need this here? You never use it...
      require ('dbconfig/dbconfig.php');
    
      // While developing
      ini_set('display_errors', 1); 
      error_reporting(E_ALL);
    
      // The details of the destination
      $username = "user";
      $password = "password";
      $urlbase = "http://domain-name/content/current/";
    
      // Create the file list
      $dateit = date("Ymd", strtotime('-1 day'));
      $files = array("aaa$dateit.tbz", "aaa$dateit.tbz.md5", "bbb$dateit.tbz", "bbb$dateit.tbz.md5");
    
      // Loop the file list
      foreach ($files as $file) {
    
        // Create pointers
        $ch = curl_init($urlbase.$file);
        $fp = fopen("adminArea/folder/$file", 'w+');
    
        // Set cURL options
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FILE, $fp);
    
        // Do it
        curl_exec ($ch);
    
        // Close pointers    
        fclose($fp);
        curl_close($ch);
    
      }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Curl to download files from location function get_data($url) { $ch =
I am trying to download nodejs tar files from server using curl.. but its
I'm writing a script to download files from an FTP server using cURL +
I am try to download image files from url from the following code, but
I have a script that download's files from a server, all works nice. But
I'm using cURL to download large XML files (between 500MB and 1GB) from a
I am trying to download a CSV file from a URL using PHP Curl,
I have to download 2.5k+ files using curl. I'm using Drupals inbuilt batch api
I have a need to rename a file after download using php cURL. Here's
I am using the following code to download files from the internet: size_t write_data(void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.