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 6962033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:36:27+00:00 2026-05-27T15:36:27+00:00

I wrote the PHP function below to download files and it works as expected.

  • 0

I wrote the PHP function below to download files and it works as expected.
However, when I try to download this file:

$url = 'http://javadl.sun.com/webapps/download/GetFile/1.7.0_02-b13/windows-i586/jre-7u2-windows-i586-iftw.exe';
download($url);

… no content is written to the file. And I can’t figure out why. The file is created, the call to curl_exec returns true, but the output file remains empty. The file can be downloaded in the browser just fine and the function successfully downloads other files. It’s just this file (host?) that I’m having problem with.

Any help is appreciated.

function download($url)
{
    $outdir = 'C:/web/www/download/';
    // open file for writing in binary mode
    if (!file_exists($outdir)) {
        if (!mkdir($outdir)) {
            echo "Could not create download directory: $outdir.\n";
            return false;
        }
    }
    $outfile = $outdir . basename($url);
    $fp = fopen($outfile, 'wb');
    if ($fp == false) {
        echo "Could not open file: $outfile.\n";
        return false;
    }
    // create a new cURL resource
    $ch = curl_init();
    // The URL to fetch
    curl_setopt($ch, CURLOPT_URL, $url);
    // The file that the transfer should be written to
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $header = array(
        'Connection: keep-alive',
        'User-Agent: Mozilla/5.0',
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    // downloading...
    $downloaded = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    fclose($fp);

    if (!$downloaded) {
        echo "Download failed: $error\n";
        return false;
    } else {
        echo "File successfully downloaded\n";
        return $outfile;
    }
}
  • 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-27T15:36:28+00:00Added an answer on May 27, 2026 at 3:36 pm

    That url redirects to another. You need to set CURLOPT_FOLLOWLOCATION to 1 for that to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php function I wrote that will take a text file and
I wrote a PHP code like this $site=http://www.google.com; $content = file_get_content($site); echo $content; But
i have created below function in a file info.php to debug variable & data
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
A couple of questions, first with this var string below, the 1 one works
I wrote a small PHP script to edit the site news XML file. I
I want to include jquery.js in myjs.js file. I wrote the code below for
Can a function inside the functions.php file call another function from within functions.php? I'm
Below is part of a PHP database class someone else wrote, I have removed
I am trying to write a php function to stop MySQL injection attempts. What

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.