Jump to content

Recommended Posts

Posted

Hello,

 

I have a very newbish question that I cant put my finger on and hoping to find some guidance here.

 

I have a script that gets a log file from an offsite ftp download. The script is setup to resume the download each time it is run so it doesnt have to redownload the complete file over and over. However, Im getting an error the 2nd time i run the script. Basically, the ftp_get php function fails to run.

 

Now, when I login via FTP and try to change permissons on the file (regarless if it was 644 or 777), I get a 'bad file descriptor' message from the ftp and it wont change any of the permissions.

 

Do I have to submitt a ticket to the helpdesk or something to change permissions on this directory? Im at aloss. :)

  • 2 weeks later...
Posted

Well, i did open a ticket and the permissions were changed from '99' back to user permissions. However, I dont think that is my issue here. At least, only part of my issue here. I think ive isolated it to be the FTP retreival.

 

Let me explain furthur..mabye some PHP guru's can see an issue with what Im doing.

 

Im trying to FTP a log file from a remote server to the local webserver via PHP. In order to do this I need to have a file on the webserver to download it to. Now, in code, if the file doesnt exist locally I use the fopen() function in PHP to create the file so it has somewhere to write to. Ive noticed when I do this that the ownership of the file I have created is set to '99' (nobody) because thats just the way it is when u create via PHP script.

 

Everything to this point is fine and dandy. The FTP grabs the file remotley and it is downloaded to the webserver no problem...here is the results i get...

 

Trying to create new local log file../logfiles/2.log

../logfiles/2.log was created sucessfully

NOTICE : Connected to xx.xx.xx.xx, for user xxxxxxxx

 

/path/to/Server_mp.log exists @ size: 6447575

Dest: ../logfiles/2.log File: Server_mp.log LocalSize: 0

Download Complete

 

So, the file (even with permissions of 99) holds the FTP data i gathered from the remote file. My parser can read from it and no issues. The new file on my webserver is called '2.log' and it has the appropriate file size with. The problem becomes when I try to resume the FTP from the previous position.

 

NOTICE : Connected to xx.xx.xx.xx, for user xxxxxxxx

 

/path/to/Server_mp.log exists @ size: 6492055

Dest: ../logfiles/2.log File: Server_mp.log LocalSize: 6447575

Parse Error : 2

ftp_get(): File transfer failed

IN /home/owner/public_html/path/to/functions.php

 

 

Here is the function that I use to FTP the file and do all the work. Ive tried doing this multiple ways with multiple approaches and they all fail on file resume. I dont think its a file permissions error at this point because even when i manually FTP up a '2.log' file with non-nobody permissions and try to resume, I still get an error on ftp_get().

 

 

>function ftpConnect($ip, $user, $pass, $path, $file, $dest)
{
//check validity
$i = 0;
if(file_exists($dest))
	{	//check for the current file
		$local_file_size = filesize($dest);
		if($local_file_size)
			rw("\n$dest exists @ size: $local_file_size");			
	}
else
	{	//try creating the file...
		rw("\nTrying to create new local log file".$dest);
		
		if ($fileconn  =  @fopen($dest,  'w')) 
		{	chmod($dest, 0666);
			fputs($fileconn,  "");
			fclose($fileconn);			
		}
		else
			trigger_error("Unable to create a local file", E_USER_ERROR);
		
		
		//clear cache
		clearstatcache();
		//now check the file again
		if(file_exists($dest))
		{	//now lets get the file size!
			rw("\n$dest was created sucessfully");				
			$local_file_size = filesize($dest); //we know its there dont care if its 0
		}
		else
			trigger_error("Unable to create a local file", E_USER_ERROR);
	}

//clear cache
clearstatcache();	

//disable script timeout
set_time_limit(0); 	

// set up basic connection
$conn_id = ftp_connect($ip);	

// login with username and password
$login_result = ftp_login($conn_id, $user, $pass); 

// check connection
if ((!$conn_id) || (!$login_result))
	trigger_error("FTP Connection has failed on $ip for user $user", E_USER_ERROR);
else
	trigger_error("Connected to $ip, for user $user", E_USER_NOTICE);
	
// Get remote filesize
if(ftp_chdir($conn_id, $path))
{	//size of file
	$remote_file_size = ftp_size($conn_id, $file);		
	//check validity
	if($remote_file_size)
		rw("\n$path$file exists @ size: $remote_file_size");	
		
	//check the remotesize							
	if ( $remote_file_size == -1 || $remote_file_size < $local_file_size )
	{	rw("\nRemote filesize of $remote_file_size < $local_file_size, log file reset!");	
		// Reset logfilesize
		$local_file_size = 0;
	}
	
	//clear cache
	clearstatcache();		
	
	rw("\nDest: $dest File: $file LocalSize: $local_file_size");
	// try to download $server_file and save to $local_file		
	if(is_readable($dest))
	{	//can we read the file?
		if(ftp_get($conn_id, $dest, $path.$file, FTP_BINARY, $local_file_size))
			rw("\nDownload Complete");
		else
			rw("\nError loading file");
	}
	else
		trigger_error("Unable to read from local file", E_USER_ERROR);		 
}
else
	trigger_error("Error changing to directory $path", E_USER_ERROR); 
// close the FTP stream and doc stream
ftp_close($conn_id);	
//reenable script time out
set_time_limit(30);

 

The most annoying part about this is that the file will be deleted once it fails. So, I have to redownload a huge log file in the event it fails. Me no likey.

 

Suggestions?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...