How to download a file for specific users

0 votes

To download a file from a third-party website, I use code similar to that in the first section below. The token is necessary, and the download will not proceed if it is invalid. With my own site, I wish to replicate that action. Although I am in charge of both the transmitting and receiving servers, I am unable to make this function. Please tell me what I'm doing incorrectly.

The code on the sending server:

    $url = "https://example.com/download/index.php?token=$someid";

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '1'); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
    curl_exec($ch);

    try {
        if(curl_errno($ch)){
            throw new Exception(curl_error($ch));
        }
    }
    catch (Exception $e){ echo $e->getMessage(); } 

    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    curl_close($ch);

    if ($statusCode == 200) {
      handle the downloaded file
    } else {
      echo 'Result = '. $statusCode; 
    }

And this is on my website (the receiving server). When I run the code above, I should get a 404 error, but all I get is "Result =" because the true is only there for testing.

    if (true || ! empty($_GET['token'])) {
        header("HTTP/1.1 404 Not Found");
        exit;
    } 
    header("Location: http://example.com/myfile.zip");
    exit; 

Can someone please help me with this?

Aug 7 in PHP by Kithuzzz
• 11,040 points
12 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

How to create and download a csv file from php script?

Hello @kartik, You can use the built in fputcsv() for ...READ MORE

answered Oct 27, 2020 in PHP by Niroj
• 82,780 points
4,245 views
0 votes
1 answer

How to Check for a Specific Type of Object in PHP

Hello @kartik, You can use instanceof: if ($pdo instanceof PDO) ...READ MORE

answered Oct 30, 2020 in PHP by Niroj
• 82,780 points
478 views
0 votes
1 answer

How to generate file for download then redirect using php?

Hii, I don't think this can be done ...READ MORE

answered Oct 30, 2020 in PHP by Niroj
• 82,780 points
3,630 views
0 votes
1 answer

How to remove a child with a specific attribute, in SimpleXML for PHP?

Hii, Just unset the node: $str = <<<STR <a> ...READ MORE

answered Nov 5, 2020 in PHP by Niroj
• 82,780 points
1,869 views
0 votes
1 answer

How to install a specific version of package using Composer?

Hello, Just use php composer.phar require For example : php ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,780 points
2,810 views
0 votes
0 answers
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14 in Others by narikkadan
• 18,480 points
45 views
0 votes
1 answer

How to fix "Headers already sent" error in PHP

The functions that send and modify the ...READ MORE

answered Feb 17 in Others by Aditya
• 7,660 points
942 views
0 votes
0 answers

PHP header(Location: ...): Force URL change in address bar

I have a new webpage but the ...READ MORE

Jun 1 in PHP by Kichu
• 19,040 points
368 views
0 votes
0 answers

PHP generate file for download then redirect

I have a PHP app that creates ...READ MORE

Jun 20 in PHP by narikkadan
• 18,480 points
77 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP