Could someone give me an example of a simple copy file script.

Discussion in 'PHP' started by Imozeb, Jul 22, 2010.

  1. #1
    How would I copy the image from $prepicurl to $varpicurl without deleting the image $prepicurl?


    $prepicurl = '../images/premade/premade_test.gif';
    $varpicurl = '../uploads/img/test.gif';
    $newimg = 'img'.mt_rand(0,1000).mt_rand(0,999).'.gif';
    $copyimage = copy($prepicurl, $newimg);
    $saveimage = rename("../images/premade/$newimg", $varpicurl);
    PHP:
    Thanks.
     
    Imozeb, Jul 22, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Copying the image files or the memory? (as in the resource)
     
    Deacalion, Jul 22, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Copy the actual file itself (.gif). Basically what I need is like rename(loc1,loc2) to move the file but what I need is the file to be duplicated so I can still have it in directory 1 yet the same file is in directory 2 under the new name.

    Before:
    Directory 1 --> bob.gif
    Directory 2 --> (empty)

    After:
    Directory 1 --> bob.gif
    Directory 2 --> foo.gif (foo.gif is the same file as bob.gif it is just under a new name)
     
    Imozeb, Jul 22, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
    $file = file_get_contents("bob.gif");
    
    file_put_contents("foo.gif", $file);
    
    ?>
    PHP:
    or

    <?php
    
    copy('bob.gif', 'foo.gif');
    
    ?>
    PHP:
    This will create a copy of bob.gif except under a different name (foo.gif), feel free to change it accordingly such as paths/directories etc.
     
    danx10, Jul 22, 2010 IP
  5. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This duplicates the file bob.gif (thats in directory1) to directory2 with the name foo.gif. The directories must already exist (although you could just use mkdir()). :)

    
    <?php
        $path = dirname(__FILE__).'/';
    
        copy($path.'directory1/bob.gif', $path.'directory2/foo.gif') || die('Failed to copy');
    ?>
    
    PHP:
     
    Deacalion, Jul 22, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks. It worked.
     
    Imozeb, Jul 22, 2010 IP