How to copy/duplicate a file?

Discussion in 'Programming' started by jimmychen, Jul 1, 2008.

  1. #1
    Hi programming gurus,

    Would like to get some help on this problem.

    I'm using php to make a copy of a file.
    After copying, I realized two things.

    1. The new file does NOT the same chmod permission as the original
    (It's 0644 instead)

    2. The new file does NOT the same owner/group as the original
    (It's null/empty instead)

    I can't use chmod, chgrp or chown on the new file because it is 0644.

    Can anyone help me?
    I just want a simple command to duplicate a file.

    Cheers!

    Jimmy
     
    jimmychen, Jul 1, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    What method did you use to copy the file, standard php function 'copy' ?

    regards
     
    Vooler, Jul 1, 2008 IP
  3. o_rly

    o_rly Guest

    Messages:
    35
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    how are you declaring your resource (handles)? that's where you want to set your permissions.
     
    o_rly, Jul 1, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    If the standard function 'copy' is causing problem. Then we may give a try to following (make sure files you copy are not too large and are not very often copied):


    #read first
    $data = implode("",file($source_file_path)); //equvilant to 'file_get_contents', all php4 dist. dont have this
    
    
    #write copy
    $f = fopen($target_file_path,"w");
    fwrite($f,$data);
    fclose($f);
    
    
    #then chmod
    chmod($target_file_path, 0777);
    Code (markup):


    I hope this helps.

    regards
     
    Vooler, Jul 1, 2008 IP
  5. jimmychen

    jimmychen Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are right. I just use the copy function.
    Will test out your code.
    Thanks mate!

    Jimmy
     
    jimmychen, Jul 1, 2008 IP
  6. jimmychen

    jimmychen Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok now copied file has the SAME permission as the source!
    But it's Owner/Group is still null/empty.

    When I try to run chown or chgrp, it gave these error message:
    "Operation not permitted"

    Why can't I change the owner/group of the file with 777?

    Any advice?

    Jimmy
     
    jimmychen, Jul 1, 2008 IP
  7. jimmychen

    jimmychen Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Anyone?.. help..
     
    jimmychen, Jul 2, 2008 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    The folder you are copying to, does that folder have permissions already set. Please check that first through FTP. And then let me know results.

    regards
     
    Vooler, Jul 2, 2008 IP
  9. jimmychen

    jimmychen Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes! The folder is set to 777.
    I just want to duplicate a file to the same folder (with permission, owner, group intact). Why it is so hard to do so? Argh...
     
    jimmychen, Jul 2, 2008 IP
  10. kenji123

    kenji123 Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #10
    Try using FTP connection to copy the file.


     
    kenji123, Jul 3, 2008 IP
    jimmychen likes this.
  11. jimmychen

    jimmychen Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks mate!

    That helps a lot :)
    Since I'm exposing my username/password of my server within the code, any ideas how to secure it?
     
    jimmychen, Jul 3, 2008 IP
  12. kenji123

    kenji123 Member

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #12
    The only method I can think of is to upload the script to the server when you need to use it, and delete it afterwards. Otherwise I don't think there's much other ways to secure it, but there might be.
     
    kenji123, Jul 4, 2008 IP