SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Thread: copy files

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    copy files

    Hi

    Is it possible to copy files directory from my pc to server??

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    It might be an obvious question, but would anything speak against using a FTP program?
    Filezilla is one I can recommend and it is well documented.
    Once you have connected to your server via FTP, it is just a matter of dragging the appropriate files and/or directories from one window to the next.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this program i asked if this can done using php,I want to exclude files

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    You want to copy files from your PC to a remote server using PHP?

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to upload directory files from local PC to Server and I want to exclude some files

    Is this possible Using php?

    if not how can I do it?

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi,

    I hope I'm understanding you correctly.
    You can use use FTP Function to copy a file from local to remote.
    For example:

    PHP Code:
    <?php
    $connect 
    ftp_connect('111.111.111');  //connect to server

    $login ftp_login($connect'ftp_username''ftp_password'); 

    if(
    $login

        if(
    ftp_put($connect'/home/user/john/test.txt','test.txt'FTP_BINARY)) 
        { 
            echo 
    "Success";
        } 


    ftp_close($connect); 
    ?>
    See here: http://stackoverflow.com/questions/9...-server-in-php

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this will upload one file,i want to upload directory

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by altarek View Post
    this will upload one file,i want to upload directory
    This is true.
    You can however, use readdir to read a list of all files in a directory into an array (minus the ones you don't want).
    Then you could modify the code I just posted to loop over the array and upload them one by one.

    Here's an example:

    PHP Code:
    $directory="exampledir";
    $dirhandler opendir($directory);
    $numFiles=0;
    while (
    $file readdir($dirhandler)) {
      if (
    $file != '.' && $file != '..'){
        
    $numFiles++;
        
    $files[$nofiles]=$file;                
      }   
    }
    closedir($dirhandler); 
    It's explained a bit better here: http://codestips.com/php-read-directory-into-array/

    Hope that helps.

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry,code at server , how to define directory at local pc?
    can you give example to write directory?

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,
    I have an awful feeling that we are talking at cross purposes.
    Are you asking me to provide an example of how to upload a directory (including the files contained within it), from your local PC to a remote server?

  11. #11
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes

  12. #12
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    I'm away from the PC now, so if no one else replies in the meantime I'll post some code later.

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hey,

    I'm back now.
    Here's the code.

    All this will do is read the files from a specified directory into an array, then it will connect to a remote server, make a directory with the same name as the one it read from and finally transfer all of the files from the local directory to the remote one.

    However, the script is very brittle and implements minimal error checking. It is more so as to show you how such a thing would be done.

    Hope this helps.

    PHP Code:
    <?php 
    $dir 
    "temp";
    $path "C:/xampp/htdocs/";
    $dirhandler opendir($path.$dir);
    $numFiles=0;
    while (
    $file readdir($dirhandler)) {
      if (!
    is_dir($file)){
        
    $numFiles++;
        
    $files[$numFiles]=$file;                
      }   
    }
    closedir($dirhandler);
     
    echo 
    "<p><strong>Local path:</strong> $path$dir</p>";
    echo 
    "<p>Found following files:<br>==============<br>";
    foreach(
    $files as $file) {
      echo 
    "$file<br>";
    }
    echo 
    "</p>";

    function 
    customError($errno$errstr)  {
      echo 
    "<b>Error:</b> [$errno$errstr<br>";
      echo 
    "Ending Script";
      die();
    }
        
    set_error_handler("customError");

    //Assumes dir doesn't exist remotely
    $connect ftp_connect('www.url.com');
    $login ftp_login($connect'user''password');  
    if(
    $login){  
      
    ftp_mkdir($connect$dir);
      
    ftp_chdir($connect$dir);
      foreach(
    $files as $file) {
        
    ftp_put($connect$file$fileFTP_BINARY);
      }
    }  
    ftp_close($connect);  
    echo 
    "<p>Transfer complete!</p>";

  14. #14
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no ,can't connect to remote server

  15. #15
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by altarek View Post
    no ,can't connect to remote server
    Oh dear!

    Have you filled these two lines out with your details?

    PHP Code:
    $connect ftp_connect('www.url.com');
    $login ftp_login($connect'user''password'); 

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes but can't
    Error: [2] ftp_connect():

  17. #17
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Bizarre.
    Can you connect to the remote server using an FTP program?

  18. #18
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes

  19. #19
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Sounds like a firewall / server configuration issue.
    Try putting this line directly after the login:

    PHP Code:
    ftp_pasv($connecttrue); 
    So it looks like this:

    PHP Code:
    $connect ftp_connect('www.url.com');
    $login ftp_login($connect'user''password'); 
    ftp_pasv($connecttrue);
    if(
    $login){
    ... 

  20. #20
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks ,connected true ,it's upload parent directory,but can't upload sub folders and files
    Error: [2] ftp_mkdir(): Create directory operation failed.

  21. #21
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi,

    Glad you got the first problem solved

    The script was just designed to make one directory on the remote server and upload a list files to it.
    Sub directories would require recursion, which would get tricky.

    Can you try this script with just one folder on your local PC which contains only files.

    Before running it, please also, make sure that there is no folder of the same name on the remote server (i.e. if your local folder's name is "test", make sure that there is no "/test" on remote).

    Let me know how you get on.


Bookmarks

Posting Permissions

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