Jump to content

Using PHP to Download/Upload automatically?


virtuexru

Recommended Posts

Maybe I'm in the wrong spot, not sure if I could do this with PHP. Here is my basic problem:

 

I have a link that is connected to an excel file. The link was made through a Javascript Popup type thing:

 

        <a href="javascript:document.csv.submit()" title="Data">Link</a>
        <form name="csv" method="post" action="http://whatever.com/data.cgi">
        <input type=hidden name=code value="X">
        <input type=hidden name=key value="KEYHERE">
        <input type=hidden name=number value="555">
        </form>

 

Basically, my problem is that I have to periodically DOWNLOAD this file, then UPLOAD it to my server to keep my data updated (about once an hour).

 

I would like to figure out a way to be able to automatically download the data then upload it via FTP or web or something. Anyone have any ideas? Thank you so much in advance :).

Figured it out! Only halfway though. I got the CSV Excel file to print out on a page. Now, how would I be able to save the file?

 

Here is my code:

 

<?php        
// target
$url   = "http://whatever.com/";

// spoof
$refer = "http://whatever.com/";

$ch = curl_init();

// set the target url
curl_setopt($ch, CURLOPT_URL, $url);

// referrer
curl_setopt($ch, CURLOPT_REFERER, $referer);

// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

// follow
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

// return
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// how many parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// extra params
curl_setopt($ch, CURLOPT_POSTFIELDS, "cc=ASD&key=xxx1234&by=1234134");

// log errors
$error = curl_error($ch);

// execute curl,fetch the result and close curl connection
$result = curl_exec ($ch);

// create new file
$fp = fopen("data.csv", "x");

// THIS DOES NOT WORK...
curl_setopt($ch, CURLOPT_FILE, $fp);

// close connection
curl_close ($ch); 

// display result
print $error;
?>

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

 

Taken from header

 

<?php        
// target
$url   = "http://whatever.com/";

// spoof
$refer = "http://whatever.com/";

$ch = curl_init();

// set the target url
curl_setopt($ch, CURLOPT_URL, $url);

// referrer
curl_setopt($ch, CURLOPT_REFERER, $referer);

// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

// follow
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

// return
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// how many parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// extra params
curl_setopt($ch, CURLOPT_POSTFIELDS, "cc=ASD&key=xxx1234&by=1234134");

// log errors
$error = curl_error($ch);

// execute curl,fetch the result and close curl connection
$result = curl_exec ($ch);

// create new file
$fp = fopen("data.csv", "x");

// THIS DOES NOT WORK...
curl_setopt($ch, CURLOPT_FILE, $fp);

// close connection
curl_close ($ch); 

header('Content-type: application/csv');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="download.csv"');

// display result
print $error;
?>

 

Not sure on the application/csv, but yea. That should give you an idea what to look for if it does not work.

Not sure on the application/csv, but yea. That should give you an idea what to look for if it does not work.

 

It worked. You are the man! Here's what I did:

 

header("Content-type: application/vnd.ms-excel");
header("Content-disposition:  attachment; filename=" . date("Y-m-d").".csv");
readfile('data.csv');

Hey virtuexru,

 

Here's a couple links that might help. Here is a function that will force a download of any file http://www.solutionbot.com/2009/01/06/php-force-download-file/ and here is a secure file uploader http://www.solutionbot.com/2008/12/27/secure-file-upload/. Check out some of what I wrote. It is very important to securely upload files as this is a wide open door for hackers if your not careful.

Archived

This topic is now archived and is closed to further replies.

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