TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-09-2008, 09:42 PM   #1 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default Kidnapping files via URLs with cURL :)

Hello there :)

I´m playing around with cURL to learn some new things.

Anyway i just wanted to share a simple code snippet i came up with when playing around. Nothing revolutionary but handy.

I´t will ofcourse need optimization to fitt your individual needs.
But in my case i was looking for a way to "download" image files to my server and store them there as files.

And i could not use URL wrapers with the conventional ways because of secrity measures by my webhost. So i thought why not try to use cURL and to my suprise the code worked. And this code is just barebone.

You are welcome to leave some tips on improvement on this code.

Here goes nothing:
PHP Code:
<?PHP
// Set Target URL
$ch curl_init('http://www.talkphp.com/images/talkphp/talkphp_logo.jpg');

// Set cURL options

curl_setopt($chCURLOPT_FRESH_CONNECT1);
curl_setopt($chCURLOPT_FORBID_REUSE1);
curl_setopt($chCURLOPT_RETURNTRANSFER1);

// Change to TRUE for BINARY transfer
curl_setopt($chCURLOPT_BINARYTRANSFER1);

// in $result is our string with the return data
// we also execute the cURL
$result curl_exec($ch); 

curl_close($ch); // Closing connection

// create a file if note present for read/write
$handle fopen('random_img.jpg''w+');

// save data to a file on server
fwrite($handle$result);

// close the file
fclose($handle);

// see if the file works ?
echo('<IMG SRC="random_img.jpg" />');
?>
Remember my webhost does currently not use PHP5 so i could not use any of thoose CURLOPT_xxx options.

Should one use the BINARY option set to TRUE if your retrieving text files like HTML, PHP, XML and so on ?

Use my code improve it, or whatever you like :)

Good Luck!

/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 05-09-2008, 11:20 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Thanks for posting up your code snippet! When simply using cURL to grab a file and save it to the local file system, there is the option to tell cURL to do that rather than making us do it with fwrite; that option is CURLOPT_FILE.

PHP Code:
<?php

// Initialize handlers
$ch curl_init('http://www.talkphp.com/images/talkphp/talkphp_logo.jpg');
$fh fopen('misc.jpg''w+');

// Set cURL to save to a file (normally stdout)
curl_setopt($chCURLOPT_FILE$fh);

// Execute the request
curl_exec($ch);

// Close handlers
curl_close($ch);
fclose($fh);

// Display that sexy logo!
echo '<img src="misc.jpg" />';
Salathe is offline  
Reply With Quote
Old 05-10-2008, 09:03 AM   #3 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

@Salathe

Thx for the feedback. :) i was unsure how to use that OPTION but i had it in my mind. So i went with fwrite.

Is there any advantage or disadvantage with the either alternatives ?

cheers :) have a nice weekend.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:14 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design