Jump to content

cURL download to user?


Cory94bailly

Recommended Posts

Hi guys, I'm currently working on a script to download files to a user..

 

What would work best (and FASTEST)? Downloading the file to the server then serving to the user OR having it go straight to the user?

 

I know obviously that it going straight to the user would be best but I'm not sure how to with cURL..

 

 

And yes, it must be with curl because I'm using a login and cookie..

Link to comment
Share on other sites

Quick example, from google. good luck!

 

<?php
        $a=0;
        $timevalue = substr(time(),3,11);
$photoname = $timevalue.$a.'.jpg';
$ch = curl_init("http://www.somedomain.com/images/some%20folder/some%20image%20file.jpg");
$fp = fopen($photoname, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

Link to comment
Share on other sites

Quick example, from google. good luck!

 

<?php
        $a=0;
        $timevalue = substr(time(),3,11);
$photoname = $timevalue.$a.'.jpg';
$ch = curl_init("http://www.somedomain.com/images/some%20folder/some%20image%20file.jpg");
$fp = fopen($photoname, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

 

 

I tried this and it does absolutely nothing:

$ab = curl_init($_POST['link']);
$cd = fopen($_POST['link'], "r");
curl_setopt($ab, CURLOPT_FILE, $cd);
curl_setopt($ab, CURLOPT_HEADER, 0);
curl_exec($ab);

Link to comment
Share on other sites

I was wondering if it was possible to have it go STRAIGHT from the server that I'm curl'ing into TO the user...

 

I guess downloading to my server then serving it to the user would work too but not very well because it's slower..

 

Is there any other way to server it straight to the user?

 

I could send them straight to the url but it needs the cookie (i think), because curl is logging in to a form.

Link to comment
Share on other sites

sorry but all php programmers love this code snippet, to force downloads to a user,straight forward and works very good.

 

 

http://www.phpfreaks.com/forums/index.php/topic,95433.0.html

 

thanks to OBER what a man......

 

I know header location it's just that it needs to be through cURL because I'm logging into an html form and you need the cookie to download the file..

Link to comment
Share on other sites

 

 

why because when you log into this html page you using Curl for, you should be getting the results on a page that curl logged into are YOU?

 

Yes, it is showing me the members area but if I do a header redirect, I personally do not have the cookie, the server does.. So it tells me to login.

Link to comment
Share on other sites

can it be done this way then.

 

just saw this on google interesting innit?

 

<?php
$url = 'http://www.tibia.com/logging/url'; // The URL to the accountmanagement page
$f1 = 'accnr'; // Name of field1(ON THE WEBSITE YOU'RE TRYING TO LOGIN ON!)
$f2 = 'pass'; // Name of field2(ON THE WEBSITE YOU'RE TRYING TO LOGIN ON!)
$v1 = $_POST['acc']; // Value of field1(FROM THE WEBSITE YOU'RE TRYING TO LOGIN FROM!)
$v2 = $_POST['pw']; // Value of field2(FROM THE WEBSITE YOU'RE TRYING TO LOGIN FROM!)
$find = 'Welcome to your account'; // String to search for in the page you've logged in on
$postchars = http_build_query( array($f1 => $v1, $f2 => $v2) );

$stream = stream_context_create( array('http' => array('method' => 'POST', 'header'  => 'Content-Type: application/x-www-form-urlencoded', 'content' =>  htmlspecialchars_decode( $postchars ) ) ) ); // Creates an array of the sourcecode, and inputs the values of the field1 and field2

$content = file_get_contents($url, false, $stream); // Gets the sourcecode from the new page that is loaded after the input of the values.

$search = strpos($content, $find); // Search the sourcecode for the specific string in the variable "find"

if($search === false){
echo 'The login seems to be incorrect. Please try again, or check if you have a valid account';
} // If the string wasn't found
else {
echo 'Your account is valid!';
}  // If the string was found 
?>

 

 

stream_context_create (looks promising?

 

http://us2.php.net/manual/en/function.stream-context-create.php

Link to comment
Share on other sites

can it be done this way then.

 

just saw this on google interesting innit?

 

<?php
$url = 'http://www.tibia.com/logging/url'; // The URL to the accountmanagement page
$f1 = 'accnr'; // Name of field1(ON THE WEBSITE YOU'RE TRYING TO LOGIN ON!)
$f2 = 'pass'; // Name of field2(ON THE WEBSITE YOU'RE TRYING TO LOGIN ON!)
$v1 = $_POST['acc']; // Value of field1(FROM THE WEBSITE YOU'RE TRYING TO LOGIN FROM!)
$v2 = $_POST['pw']; // Value of field2(FROM THE WEBSITE YOU'RE TRYING TO LOGIN FROM!)
$find = 'Welcome to your account'; // String to search for in the page you've logged in on
$postchars = http_build_query( array($f1 => $v1, $f2 => $v2) );

$stream = stream_context_create( array('http' => array('method' => 'POST', 'header'  => 'Content-Type: application/x-www-form-urlencoded', 'content' =>  htmlspecialchars_decode( $postchars ) ) ) ); // Creates an array of the sourcecode, and inputs the values of the field1 and field2

$content = file_get_contents($url, false, $stream); // Gets the sourcecode from the new page that is loaded after the input of the values.

$search = strpos($content, $find); // Search the sourcecode for the specific string in the variable "find"

if($search === false){
echo 'The login seems to be incorrect. Please try again, or check if you have a valid account';
} // If the string wasn't found
else {
echo 'Your account is valid!';
}  // If the string was found 
?>

 

 

stream_context_create (looks promising?

 

http://us2.php.net/manual/en/function.stream-context-create.php

 

Even if that does work (Logging in), I don't see how it would allow the user to download the 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.