0

I have local wamp server in my windows machine. Inside the www directory i have folder called project. c:www/project/index.php.

And next same path, i have directory to save the destination location c:www/project/downloads.

when i was run my localhost http://localhost:8089/curl/index.php

I have following error, error image attached here. php curl method download file

This is my code:

      <?php class download {

    const URL_MAX_LENGTH = 3000;

    protected function cleanUrl($url) {
        if(isset($url)){
            if(!empty($url)){
                if(strlen($url) < self::URL_MAX_LENGTH) {
                    return strip_tags($url);
                }
            }

        }
    }

    //is url
    protected function isUrl($url) {
    $url = $this->cleanUrl($url);
    if(isset($url)) {
        if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
            return $url;
        }
    }
}

    protected function returnExtension($url) {
        if($this->isUrl($url)) {
           $end = end(preg_split("/[.]+/", $url));
           if(isset($end)) {
                return $end;
            }
        }
    }

    public function downloadFile($url) {
        if($this->isUrl($url)) {
           $extension = $this->returnExtension($url);
           if($extension) {
               echo $url;
               $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
            $return = curl_exec($ch);

            $destination = "downloads/file.$extension";
            $file = fopen($destination, "w+");
            fputs($file, $return);
           }
            if(fclose($file)){
                echo "File Download";

            }

        }

    }

    }


$obj = new Download();
if(isset($_POST['url'])) { $url = $_POST['url']; } ?>

<form action="http://localhost:8089/curl/index.php" method="post">

    <input type="text" name="url" maxlength="3000"/>
    <input type="submit" value="download"/>

</form>

<?php if(isset($url)) { $obj->downloadFile($url); } ?>

could you please some one help on this ?

Many Thanks, Palani

2 Answers 2

2

Replace below line at the returnExtension function

$end = end(preg_split("/[.]+/", $url));

with

$result = preg_split("/[.]+/", $url);       
$end = end($result);
Sign up to request clarification or add additional context in comments.

1 Comment

HI Jay, Thanks lot your post, now it is working file :)
0

You have to change this line:

$end = end(preg_split("/[.]+/", $url));

to

$urlParts = preg_split("/[.]+/", $url);
$end = end($urlParts);

this is not really an error, but a strict standard notice. It's because php want only variables passed by reference, and the return of preg_split is not a variable.

1 Comment

Can yo please tell how your answer is different from @Jay Gosai ??

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.