Using FTP via PHP

Discussion in 'PHP' started by adamjblakey, Nov 7, 2008.

  1. #1
    Hi,

    I am trying to upload some files using ftp via php.

    I have built a function which should work as far as i can see but it isn't. Can anyone see why this would not work?


    
    function ftpupload($file, $destination){
    	
    	// set up the settings
    	$ftp_server = "ftp.".$_SESSION['domain'];
    	$ftpuser = $_SESSION['user'];
    	$ftppass = $_SESSION['pass'];
    	
    	// delete the below variables if you decide to use a form.
    	$source_file = $file;
    	$destination_file = $destination;
    	
    	// set up basic connection
    	$conn_id = ftp_connect($ftp_server);
    	
    	// login with username and password
    	$login_result = ftp_login($conn_id, $ftpuser, $ftppass);
    	
    	ftp_put($conn_id, "$destination_file", "$source_file", FTP_BINARY); // the ftp function
    	
    	// close the connection
    	ftp_close($conn_id);  
    	
    }
    
    PHP:
    Then:

    
    ftpupload($rand_name.$file_ext, "/uploads/images/thumbs/");
    
    PHP:
    Any Ideas?
    Cheers,
    Adam
     
    adamjblakey, Nov 7, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    have you tried to output the error for example

    print_r(ftpupload($rand_name.$file_ext, "/uploads/images/thumbs/"));

    and please check if a connection is created by using

    if ($login_result !== false)
    {
    // the rest
    }
     
    EricBruggema, Nov 7, 2008 IP
  3. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #3
    Remove "" from around the ftp put parameters (file names). Also, what is the error message?
     
    Lordo, Nov 7, 2008 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Right i have given up doing it that way, i have however found a way that does upload but when the file is uploaded on the server it is 0kb but at least it is uploading.

    
    function ftpupload($file){
    	
    	$ch = curl_init();
     	$localfile = $file;
     	$fp = fopen($localfile, 'r');
     	curl_setopt($ch, CURLOPT_URL, 'ftp://'.$_SESSION['name'].':'.$_SESSION['pass'].'@87.229.14.87/httpdocs/uploads/images/thumbs/'.$file);
     	curl_setopt($ch, CURLOPT_UPLOAD, 1);
     	curl_setopt($ch, CURLOPT_INFILE, $fp);
     	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
     	curl_exec ($ch);
     	$error_no = curl_errno($ch);
     	curl_close ($ch);
            if ($error_no == 0) {
            	print 'File uploaded succesfully.';
            } else {
            	print 'File upload error.';
            }
    	
    }
    
    PHP:
    Any Ideas why it is 0kb?
     
    adamjblakey, Nov 7, 2008 IP