Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter FordPrefect99

    (@fordprefect99)

    The ftp functionality appears to be far to chatty, opening many connections per file. I am hosting my ftp server on azure, and have dedicated over 130 ports to the ftp process. In ajax mode, I have yet to see more than 4 files transfer successfully at a time.
    The failures messages are red herrings.

    Plugin Author nickboss

    (@nickboss)

    Hi, I suppose you use the Pro version?

    what is the size of the files? You failed to upload more than 4 files?

    Nickolas

    I fixed the problem.
    All you need to do:
    1. Replace the wp-file-upload/lib/wfu_io.php file with the code I attached
    2. No matter what, you have to add :21 (or the ftp port you use if different of the default 21) Like: username:password@serveraddress:21 on your FTP credentials
    3. Enable Passive Mode

    Tha’s All!

    Code:

    <?php
    
    function wfu_create_directory($path, $method, $ftpdata) {
    	$ret_message = "";
    	if ( $method == "" || $method == "normal" ) {
    		mkdir($path, 0777, true);
    	}
    	else if ( $method == "ftp" && $ftpdata != "" ) {
    		$ftpdata_flat =  str_replace(array('\:', '\@'), array('\_', '\_'), $ftpdata);
    		$pos1 = strpos($ftpdata_flat, ":");
    		$pos2 = strpos($ftpdata_flat, "@");
    		if ( $pos1 && $pos2 && $pos2 > $pos1 ) {
    			$ftp_username = substr($ftpdata, 0, $pos1);
    			$ftp_password = substr($ftpdata, $pos1 + 1, $pos2 - $pos1 - 1);
    			list($ftp_host, $ftp_port) = explode(":", substr($ftpdata, $pos2 + 1));
    			$conn_id = ftp_connect($ftp_host,$ftp_port);
    			$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
    			if ( $conn_id && $login_result ) {
    				$flat_host = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $ftp_host);
    				echo $flat_host." ";
    				$pos1 = strpos($path, $flat_host);
    				echo $pos1." ";
    				if ( $pos1 ) {
    					echo $path." ";
    					$path = substr($path, $pos1 + strlen($flat_host.":".$ftp_port));
    					echo $path;
    					ftp_mkdir($conn_id, $path);
    					ftp_chmod($conn_id, 493, $path);
    				}
    				else {
    					$ret_message = WFU_ERROR_ADMIN_FTPDIR_RESOLVE;
    				}
    			}
    			else {
    				$ret_message = WFU_ERROR_ADMIN_FTPINFO_INVALID;
    			}
    			ftp_quit($conn_id);
    		}
    		else {
    			$ret_message = WFU_ERROR_ADMIN_FTPINFO_EXTRACT;
    		}
    	}
    	else {
    		$ret_message = WFU_ERROR_ADMIN_FTPINFO_INVALID;
    	}
    	return $ret_message;
    }
    
    function wfu_upload_file($source, $target, $method, $ftpdata, $passive, $fileperms) {
    	$ret_array = "";
    	$ret_array["uploaded"] = false;
    	$ret_array["admin_message"] = "";
    	$ret_message = "";
    	$target_perms = substr(sprintf('%o', fileperms(dirname($target))), -4);
    	$target_perms = octdec($target_perms);
    	$target_perms = (int)$target_perms;
    	if ( $method == "" || $method == "normal" ) {
    		$ret_array["uploaded"] = move_uploaded_file($source, $target);
    		if ( !$ret_array["uploaded"] && !is_writable(dirname($target)) ) {
    			$ret_message = WFU_ERROR_ADMIN_DIR_PERMISSION;
    		}
    	}
    	elseif ( $method == "ftp" &&  $ftpdata != "" ) {
    		$result = false;
    		$ftpdata_flat =  str_replace(array('\:', '\@'), array('\_', '\_'), $ftpdata);
    		$pos1 = strpos($ftpdata_flat, ":");
    		$pos2 = strpos($ftpdata_flat, "@");
    		if ( $pos1 && $pos2 && $pos2 > $pos1 ) {
    			$ftp_username = substr($ftpdata, 0, $pos1);
    			$ftp_password = substr($ftpdata, $pos1 + 1, $pos2 - $pos1 - 1);
    			list($ftp_host, $ftp_port) = explode(":", substr($ftpdata, $pos2 + 1));
    			$conn_id = ftp_connect($ftp_host,$ftp_port);
    			$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
    			if ( $conn_id && $login_result ) {
    				$flat_host = preg_replace("/^(.*\.)?([^.]*\..*)$/", "$2", $ftp_host);
    				$pos1 = strpos($target, $flat_host);
    				if ( $pos1 ) {
    					if ( $passive == "true" ) ftp_pasv($conn_id, true);
    //					$temp_fname = tempnam(dirname($target), "tmp");
    //					move_uploaded_file($source, $temp_fname);
    					$target = substr($target, $pos1 + strlen($flat_host.":".$ftp_port));
    //					ftp_chmod($conn_id, 0755, dirname($target));
    					$ret_array["uploaded"] = ftp_put($conn_id, $target, $source, FTP_BINARY);
    					//apply user-defined permissions to file
    					$fileperms = trim($fileperms);
    					if ( strlen($fileperms) == 4 && sprintf("%04o", octdec($fileperms)) == $fileperms ) {
    						$fileperms = octdec($fileperms);
    						$fileperms = (int)$fileperms;
    						ftp_chmod($conn_id, $fileperms, $target);
    					}
    //					ftp_chmod($conn_id, 0755, $target);
    //					ftp_chmod($conn_id, $target_perms, dirname($target));
    					unlink($source);
    					if ( !$ret_array["uploaded"] ) {
    						$ret_message = WFU_ERROR_ADMIN_DIR_PERMISSION;
    					}
    				}
    				else {
    					$ret_message = WFU_ERROR_ADMIN_FTPFILE_RESOLVE;
    				}
    			}
    			else {
    				$ret_message = WFU_ERROR_ADMIN_FTPINFO_INVALID;
    			}
    			ftp_quit($conn_id);
    		}
    		else {
    			$ret_message = WFU_ERROR_ADMIN_FTPINFO_EXTRACT.$ftpdata_flat;
    		}
    	}
    	else {
    		$ret_message = WFU_ERROR_ADMIN_FTPINFO_INVALID;
    	}
    
    	$ret_array["admin_message"] = $ret_message;
    	return $ret_array;
    }
    
    ?>

    Plugin Author nickboss

    (@nickboss)

    Dear lobsterdog thanks for the proposal,

    I will put your additions (port number) in the next release. Is there are reason for these two echo commands in wfu_create_directory function?

    Nickolas

    The echos were my debug. Sorry, I guess I forgot them there.
    Thank you,

    Plugin Author nickboss

    (@nickboss)

    Ok

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘FTP create directory does not work properly’ is closed to new replies.