abazoskib Posted July 23, 2009 Share Posted July 23, 2009 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 More sharing options...
celsoendo Posted July 23, 2009 Share Posted July 23, 2009 Where is the php error generated when the dir exists? Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-880903 Share on other sites More sharing options...
abazoskib Posted July 23, 2009 Author Share Posted July 23, 2009 The error followed: ftp_get could not open file: .... fopen could not open file stream: ... and so on through my loop to get all the files in that directory. It wouldn't even download the files. Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-880935 Share on other sites More sharing options...
chronister Posted July 23, 2009 Share Posted July 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-880969 Share on other sites More sharing options...
Bendude14 Posted July 23, 2009 Share Posted July 23, 2009 can you post the code that calls this method also createBackupDirs($sourceArr) Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-880977 Share on other sites More sharing options...
abazoskib Posted July 23, 2009 Author Share Posted July 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-880988 Share on other sites More sharing options...
Bendude14 Posted July 23, 2009 Share Posted July 23, 2009 try echoing $localDir out inside the loop before you try to open it, either you have the wrong path to the file or the wrong permissions on the file. Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-881016 Share on other sites More sharing options...
abazoskib Posted July 23, 2009 Author Share Posted July 23, 2009 Yea I tried that and the paths looked fine. Like I said when I removed the call to createBackupDirs the script ran fine. Strange. Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-881130 Share on other sites More sharing options...
abazoskib Posted July 24, 2009 Author Share Posted July 24, 2009 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? Link to comment https://forums.phpfreaks.com/topic/167063-creating-ftp-directories-script-broken/#findComment-881767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.