Upload from url via php (php script)

Discussion in 'PHP' started by zahiid, May 3, 2014.

  1. #1
    Hi,
    I have a simple script below which has been working fine to upload files from external server. I find it useful in many times.

    create a directory "downloads" , change permission to writable,
    create a php file, "ul.php" , paste the code and save.
    you are done.

    <form action="ul.php" method="post">
    2
    Enter URL: <input type="text" name="url" size="35" /> <input type="submit" value="Submit" name="submit" />
    3
    </form>
    <?
    if($_POST["submit"]){
    
    $url = trim($_POST["url"]);
    
    if($url){
    $file = fopen($url,"rb");
    if($file){
    
    $directory = "downloads/"; // Directory to upload files to.
    $valid_exts = array("avi","zip","rar","png"); // default image only extensions
    $ext = end(explode(".",strtolower(basename($url))));
    if(in_array($ext,$valid_exts)){
    
    $rand = rand(jadukor.com,99999);
    $filename = jadukor.com . basename($url);
    $newfile = fopen($directory . $filename, "wb"); // creating new file on local server
    
    if($newfile){
    while(!feof($file)){
    
    // Write the url file to the directory.
    fwrite($newfile,fread($file,1024 * 8),1024 * 8); // write the file to the new directory at a rate of 10mb/sec. until we reach the end.
    
    echo 'File uploaded successfully! You can access the file here:<br/>';
    echo '<a href="'.$directory.$filename.'" target="_blank">'.$directory.$filename.'</a>';
    
    }
    } else { echo 'Could not establish new file ('.$downloads.$rand.basename($url).') on local server. Be sure to CHMOD your directory to 777.'; }
    } else { echo 'Invalid file type. Please try another file.'; }
    } else { echo 'Could not locate the file: '.$url.''; }
    
    } else { echo 'Invalid URL entered. Please try again.'; }
    }
    
    ?>
    PHP:
     
    zahiid, May 3, 2014 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Uhh, jadukor.com :confused: ???
     
    hdewantara, May 4, 2014 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    A couple of notes on this:

    1) This will not work if the host has allow_url_fopen settings set to 0. A sure way is to use fsockopen.
    2) You should always have errors/notices turned on, if you did you would see 2 undefined constants are trying to use jadukor and com (which then PHP converts to strings).
    3) Besides the notices, rand(jadukor.com,99999); is equaivalent to rand(0, 99999);
    4) Same issue with notices on the other jadukor.com.
    5) Use full php tags: <?php instead of the shortags <? as they are disabled by default on newer versions.
    6) Code formatting is very hard to follow. Use some coding standard on your code to make it more readable: http://pear.php.net/manual/en/standards.php
     
    ThePHPMaster, May 4, 2014 IP