I searching for php script (remote upload) Looking for help

Discussion in 'PHP' started by duperhost, Dec 23, 2010.

  1. #1
    am trying to find php script so I can transload files from my server to another server any help ?!

    Happy Holidays Everyone..
    Thanks and regards
     
    duperhost, Dec 23, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Well this can be coded ... upload through FTP ?
    Here is some code :
    
    <?php
        $user = 'username';
        $pass = 'password';
        $host = 'hostname';
        $port = 21;
        $localFile = 'localfile.txt';
        $remoteFile = 'remotefile.txt';
        
        $con = ftp_connect($host,$port,15) or die('Couldn\'t connect to the required ftp server');
        
        if(@ftp_login($con,$user,$pass)) {
            if( ftp_put($con,$remoteFile,$localFile,FTP_ASCII) ) {
                echo 'The file has been successfully uploaded.';
            } else {
                echo 'The file couldn\'t be uploaded !';
            }
            
            ftp_close($con);
        } else {
            echo 'Username or password is wrong !';
            ftp_close($con);
        }
    ?>
    
    PHP:
     
    tvoodoo, Dec 23, 2010 IP
    duperhost likes this.
  3. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Thank you for great script, It dose not seems that is supporting large files, got
    when trying to move a 1 GB file

    The script able to do this up to 600 MB
     
    Last edited: Dec 23, 2010
    duperhost, Dec 23, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Try this then :
    
    <?php
        set_time_limit(0);
        ini_set('upload_max_filesize','1000M');
        ob_start();
        $remote_file = 'remote.txt';
        $local_file = 'local.txt';
    
        $fp = fopen($local_file, 'r');
        $conn_id = ftp_connect($ftp_server);
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        $ret = ftp_nb_fput($conn_id, $remote_file, $fp, FTP_BINARY);
        while ($ret == FTP_MOREDATA) {
            // Establish a new connection to FTP server
            if(!isset($conn_id2)) {
                $conn_id2 = ftp_connect($ftp_server);
                $login_result2 = ftp_login($conn_id2, $ftp_user_name, $ftp_user_pass);
            }
           
            // Retreive size of uploaded file.
            if(isset($conn_id2)) {
                clearstatcache(); // <- this must be included!!
                $remote_file_size = ftp_size($conn_id2, $remote_file);
            }
    
           // Calculate upload progress
           $local_file_size  = filesize($local_file);
           if (isset($remote_file_size) && $remote_file_size > 0 ){
               $i = ($remote_file_size/$local_file_size)*100;
               printf("%d%% uploaded<br>", $i);
               flush();
           } 
            $ret = ftp_nb_continue($conn_id);
        }
    
        if ($ret != FTP_FINISHED) {
            print("There was an error uploading the file...<br>");
            exit(1);
        } else {
            print("Done.<br>");
        }
        fclose($fp);
        ob_end_flush();
    ?>
    
    PHP:
     
    Last edited: Dec 23, 2010
    tvoodoo, Dec 23, 2010 IP
  5. makeit easy

    makeit easy Active Member

    Messages:
    2,067
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    How to upload folders?
     
    makeit easy, Jan 8, 2011 IP
  6. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #6
    why dont you simply use ftp programs instead?
    if you are transferring large chances are you might hit the php ini memory limit on either side.
    still if a necessity tvoodoo solutions look good.
    Thanks
     
    ankit_frenz, Jan 8, 2011 IP
  7. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #7
    It's not possible to upload folder but you can zip the folder and upload it.

    Yes, ftp is good but for large files it's faster to do it direct but if you have bad server it would not work.

    <?php
        set_time_limit(0);
        ini_set('upload_max_filesize','1000M');
        ob_start();
    // the file on remote http example http://domainname.com/remote.txt
        $remote_file = 'remote.txt';
    // name the file in local example local.txt
        $local_file = 'local.txt';
    
        $fp = fopen($local_file, 'r');
        $conn_id = ftp_connect($ftp_server);
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        $ret = ftp_nb_fput($conn_id, $remote_file, $fp, FTP_BINARY);
        while ($ret == FTP_MOREDATA) {
            // Establish a new connection to FTP server
            if(!isset($conn_id2)) {
                $conn_id2 = ftp_connect($ftp_server);
                $login_result2 = ftp_login($conn_id2, $ftp_user_name, $ftp_user_pass);
            }
           
            // Retreive size of uploaded file.
            if(isset($conn_id2)) {
                clearstatcache(); // <- this must be included!!
                $remote_file_size = ftp_size($conn_id2, $remote_file);
            }
    
           // Calculate upload progress
           $local_file_size  = filesize($local_file);
           if (isset($remote_file_size) && $remote_file_size > 0 ){
               $i = ($remote_file_size/$local_file_size)*100;
               printf("%d%% uploaded<br>", $i);
               flush();
           }
            $ret = ftp_nb_continue($conn_id);
        }
    
        if ($ret != FTP_FINISHED) {
            print("There was an error uploading the file...<br>");
            exit(1);
        } else {
            print("Done.<br>");
        }
        fclose($fp);
        ob_end_flush();
    ?>
    PHP:
    but where did you declare the username and password and host name and port as old script
       <?php
        $user = 'username';
        $pass = 'password';
        $host = 'hostname';
        $port = 21;
    ?>
    
    PHP:
    I think it has been changed to :
     <?php
    $ftp_user_name = 'the ftp username';
    $ftp_user_pass 'the ftp password';
    $ftp_server='ftp host name or ip';
    ?>
    
    But I am not sure where is the port number..
    
    PHP:
     
    Last edited: Jan 17, 2011
    duperhost, Jan 17, 2011 IP
  8. duperhost

    duperhost Well-Known Member

    Messages:
    817
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #8
    Do you have any idea why this script dose not work ?
     
    duperhost, Aug 29, 2012 IP