0

I used this function for downloading SWF files(flash games) sucessfully. When I use this script for one particular site it downloads all games(I told the script to download 4 games from a list) with exact size of 299bytes? I tried downloading these games with Google Chrome and the download is sucessfull. Is there something missing in the CURL functions I use or the download algorithm is not good enough? Any help will be greatly appreciated.

function saveFlash($fullPaths,$folder,$gamenames,$i){
        $curl = curl_init($fullPaths[$i]);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //Create a new file in the given folder
        $fp = fopen($folder."/".$gamenames[$i].".swf", 'w');
            if ($fp == FALSE){ 
                echo "File not opened<br>";} 
        //Ask cURL to write the contents to a file
        curl_setopt($curl, CURLOPT_FILE, $fp);
        //Execute the cURL session
        curl_exec ($curl);
        //Close cURL session and file
        curl_close ($curl);
        fclose($fp);
    }

Text editor gives the following

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.freeonlinegames.com/">here</a>.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at freeonlinegames.com Port 80</address>
</body></html>
4
  • 2
    Have you looked at those 299 byte files with a text editor? Most likely it'll be "Sorry, no direct access allowed" error messages of some sort. Commented Jun 14, 2011 at 16:12
  • 2
    Your script gets blocked. Look into the returned data (should be HTML) for a reason. Commented Jun 14, 2011 at 16:13
  • It says the document has moved Commented Jun 14, 2011 at 16:14
  • <title>302 Found</title> </head><body> <h1>Found</h1> <p>The document has moved <a href="freeonlinegames.com/">here</a>.</p> <hr> <address>Apache/2.2.3 (CentOS) Server at freeonlinegames.com Port 80</address> </body></html> Commented Jun 14, 2011 at 16:14

2 Answers 2

1

You'll want to set CURLOPT_FOLLOWLOCATION to allow it to follow the redirects.

You may also want to set a CURLOPT_MAXREDIRS so it doesn't redirect out of control.

Sign up to request clarification or add additional context in comments.

Comments

1

That error you're getting is a common way of telling you that no hotlinking is allowed. If you simply want to download the SWF, you need to set the referrer.

curl_setopt($ch, CURLOPT_REFERER, 'http://urlofpagetheswfwasfoundon');

If it still doesn't work after that, you might need to set an appropriate user-agent string.

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 Something Something');

Also, be very sure that you are allowed to do what you are trying to do. Ripping stuff off others' sites is very frowned upon, and usually illegal.

4 Comments

Really want to test this lines of code because of the knowledge but is it possible for this site to track my IP and fill lawsuit against me? As a starting web developer I should know how do defend my site from CURL attacks.
Absolutely, and if you think for one minute that we are going to help you break the law, then you are on the wrong site, and are not welcome here. I have provided this example to help you learn, not to help you be an ass.
Thanks. I don't want to be an ass. Knowing more about using CURL and defending from CURL isn't something wrong. Should test this code on my server from friend's PC.
Correct. Nothing wrong with learning, which is what this information is for. You suggested that you were nervous about being tracked. If you have permission, then there is no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.