Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    how do you copy an external file (image) URL from a form to your own server?

    I am trying to copy an image that the user inputs into a form to a local copy on my server.. and for security reasons rename the file

    so basically, user inputs a URL to the image (http://www.foo.com/image.gif)
    user hits submit
    script copies http://www.foo.com/image.gif and renames it to a random md5 name

    how would I do this using PHP?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    <?php
    // this must be done somehow how you want
    $filename 'test.txt';

    $handle fopen("http://www.foo.com/""rb");
    if(
    $handle){
      
    $somecontent stream_get_contents($handle);
      
    fclose($handle);

      
    $handle fopen($filename'wb');
      if(
    $handle){
        if (
    fwrite($handle$somecontent) === FALSE) {
           echo 
    "Cannot write to file ($filename)";
           exit;
        }
        echo 
    "Success, wrote ($somecontent) to file ($filename)";
        
    fclose($handle);
      }else{
         echo 
    "Cannot create file ($filename)";
         exit;
      }
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •