Jump to content

creating ftp directories - script broken


abazoskib

Recommended Posts

private function createBackupDirs($sourceArr) { //NEEDS WORK
$today = date("mdY");
foreach($sourceArr as $source){
	if($source[2] == "Lead"){
		$dir = $source[1]."/backups/backups_".$today;
		if (!$this->cd($dir)) {
			$this->mkdir($dir);
			return true;
		}else
			return false;

	}
}
}

 

I created this function to test if a directory exists, and if it doesnt, it should create it. The problem I found was when a directory does exist, it breaks the entire script. Can anyone see why this would cause the whole script to fail if the directories already existed?

 

edit here's the cd function:

 

 public function cd($dir){
ftp_chdir($this->conn, $dir);
}

Link to comment
https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/
Share on other sites

If your looking to determine if a directory exists, then why not use is_dir?

 

 

<?php

$dir = $_SERVER['DOCUMENT_ROOT'].'/path/to/dir';

if( is_dir($dir) ){
     // keep on trucking, directory exists
  }else{
     // directory does not exist, so create that S.O.B.  
}

?>

 

After typing this up, I realize that this may not work because your connecting through FTP rather than local filesystem... but hey.. it's already typed, and may give some help.

 

Nate

is_dir does not work over ftp.

 

public function getAll($localDir,$remote,$extension,$mode){
$today = date("mdY");
$this->cd($remote);
$contents = $this->ls();
$sourceArray = AllInbox::getSources();
$this->createBackupDirs($sourceArray);
	foreach($contents as $file){
		$length = strlen($localDir);
		$ext = substr(strrchr(strtolower($file), "."), 1);
		if(($file != ".") && ($file != "..") && (!is_dir($file)) && ($file != ".htaccess") && (($ext == $extension))){			
			$localDir .= $file; // add the filename to the filepath
			$this->get($localDir,$file,$mode);
			$read = fopen($localDir, 'r'); // open the newly downloaded file
			$line = fgets($read); // read the first line to get the source
			fclose($read); // close the file

			// find which source this file belongs to
			foreach($sourceArray as $key){
				$fileSource = strpos($line,$key[1]);
				if ($fileSource === false) {
    					}else{
					$folder = $key[1];
        					break;
    					}
			}
			//echo "File: ".$file." belongs to: ".$folder."\n";

			$destFolder = $folder."/"."backups"."/backups_".$today."/".$file;
			$this->mv($file,$destFolder);
			$localDir = substr($localDir,0,	$length); // remove the filename from the filepath					
		}
	}
}

 

In my case yesterday, I had already run the script once earlier that day. So the folders backups_07222009 were already created. When I removed the call to createBackupDirs, the script ran successfully, and I was able to download and move files without a problem. So Bendude14, you are on the right track so far as I can tell.

 

looks like the return true/false had something to do with it. When i removed all the if statements from my functions and the 'return true/false' everything worked fine. of course now there is no error checking. is this a limitation of the php ftp functions?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.