Uploading a file from a URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    Uploading a file from a URL

    Hi all,
    I have a Admin page (only for me, pass required) where I want be able to upload a file from the web, to my website.
    I want to upload from a URL.
    (I can't move_uploaded_f ile() because it's not in the $_FILES array.)


    Can anybody please help me out on this?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You may be able to use the file_get_conten ts function to do this.

    It requires the allow_url_fopen directive to be enabled.

    Comment

    • moishy
      New Member
      • Oct 2006
      • 104

      #3
      I don't get what I'm supposed to do with file_
      get_contents(). ..

      Here, say I have a variable containing a file address:
      $file = "http://www.host.com/files/image123.jpg";

      And I want to upload it to:
      $target = "/files/";

      How would I use file_get_conten ts() to do that?

      Thanks guys, you are very helpful

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        You could, for example do:
        [code=php]
        <?php
        $file = "URL to your file";
        $target = "/path/to/your/save/dir/filename.ext";

        if($contents = file_get_conten ts($file)) {
        if(file_put_con tents($target, $contents)) {
        echo "Your file has been uploaded!";
        }
        else {
        echo "Failed to save the file to the target path";
        }
        }
        else {
        echo "Failed to get the file";
        }
        ?>
        [/code]

        Comment

        • moishy
          New Member
          • Oct 2006
          • 104

          #5
          Thanks for your post!

          It worked. But only when the Target path was set to the entire host directory!
          i.e. /home/content/l/i/v/livingsidrah/html/ is considered the root folder (I use godaddy hosting)

          Is there any workaround, that I won't need to type the entire directory?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            You should be able to use a relative path. Something like:
            Code:
            path/to/relative/dir/file.ext

            Comment

            • moishy
              New Member
              • Oct 2006
              • 104

              #7
              Didn't work for me:

              PHP file is located here:
              ROOT/directory/file.php

              Upload target is:
              ROOT/directory/files/uploaded_file.e xt

              I set $target to /files/uploaded_file.e xt but didn't work.

              I had the same problem with file_exists()

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                If you use a / in front of a path, a Linux system will assume that is an absolute path, starting from the root directory.

                If you want to set a relative path, that is; a path relative to the current directory, do not put a / in front of it.

                For example:
                [code=php]
                # Lets assume your code is being executed from:
                # /var/www/html/index.php

                # An absolute path
                $absolute = "/var/www/html/uploads/newfile.ext";

                # A relative path
                $relative = "uploads/newfile.ext";
                [/code]

                Comment

                Working...