Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem moving a file to a different directory - Using rename() 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I am working on a very simple script and it is giving me a hard time
Code:
	$fromfile = './files/'.$frompath.'/'.$filename;
	$tofile = './files/'.$topath.'/'.$filename;

	$ScriptName=$_SERVER['PHP_SELF'].': ';
	$LogFile=fopen("c:/temp/web.log","a+");
	if($LogFile) { $LogTxt=fputs($LogFile,$ScriptName."From File: ".$fromfile."\r\n"); }
	if($LogFile) { $LogTxt=fputs($LogFile,$ScriptName."To File: " .$tofile."\r\n"); }

	if(!rename($fromfile, $tofile)) {

		if($LogFile) { $LogTxt=fputs($LogFile,$ScriptName."Unable to move files ...\r\n"); }

		$objResponse = new xajaxResponse();
		$objResponse->alert("Unable to move document " . $fromfile . " to " . $tofile . "!");
		return $objResponse;

	} else {
		OpenDB('docsdb');
		$sql = 'UPDATE docs  SET 
		`catid` = "' . $dirid . '",
		`path` = "' . $topath . '"
		WHERE id = ' . $docid . ' LIMIT 1;';
		DoQuery($sql);
	}

After running the script, web.log has following content
Code:
/webdocs/index.php: From File: ./files/Sales/arrow.png
/webdocs/index.php: To File: ./files/Accounting/arrow.png
/webdocs/index.php: Unable to move files ...

According to php.net, the above syntax should work. Directories and file are valid.

Running on MS-XP ... I am able to create directories using mkdir() within the same directories where I am moving files from/to (and failing to), so permission is not an issue.

What say you?

Thanks!
 
does windows support the dot and double dot notations? could you try with realpath() and see what happens. and then also see whtehr it is the delete part of the move that causes problems. if this is the case then copy() should work.


 
Not sure what you mean by dot and double dots notations. I have tried using copy() with no luck. I have changed the / for \ and have changed to absolute path in lieu of relative.

Nothing so far. I added is_writeable() and it passes this check but still fails to copy() or rename().

I was not about to use a HACK and try something like
Code:
$output = pclose(popen("start /B cmd /C COPY ".$fromfile."  ".$tofile ." 2>&1", "r"));
This is something posted by chris at no_email_is_relevant dot com in
Looking into realpath() and see what I learn here ...

Thanks,
 
Are you sure permissions are not an issue? On Windows XP, I had a problem myself that mkdir would only make read-only directories. I had to shell out to dos to do a "proper" mkdir.

On Linux, I never had any problems using mkdir.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
realpath() did the job ... It helped ensure that I was not leaving / where \ were needed.

I also ended up using this UDF
Code:
function rename_win($oldfile,$newfile) {
   if (!rename($oldfile,$newfile)) {
      if (copy ($oldfile,$newfile)) {
         unlink($oldfile);
         return TRUE;
      }
      return FALSE;
   }
   return TRUE;
}

called as if(!@rename_win("$fromfile", "$tofile"))

Program is moving forward at last and I can now see the light at the end of the tunnel.


Thank you so very much!

 
DonQuichote,

I am using if(is_writable($dirA) && is_writable($dirB))
as a condition to attempt the move/copy. If FALSE, I am executing a series of commands and so far, condition is always TRUE.

I will continue to test with other directories created by using mkdir and directory names with space (i.e.: hello world in lieu of hello_world).

Thanks,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top