By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Moving files by using the function rename()

P: 61
Hi,
I am new to PHP and trying to move a zip file from one directory to another. I researched on the web on this and all fingers pointed to this rename() function.

I tried writing a PHP script which simply moves a zip file (located in the same directory as the source code) to another directory by:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $sourcecode = dirname(__FILE__).'\zipfile.zip'; //appending the current directory with the file name
  3.  
  4. $destination = 'C:\xampp\htdocs';
  5.  
  6. rename($sourcecode, $destination);
  7. ?>
  8.  
I have already tested whether the $sourcecode has the correct path to the file. Upon executing it, my browser gives me this error:

Warning: rename(C:\xampp\htdocs\tutorial\zipfile.zip,C:\xam pp\htdocs) [function.rename]: File exists in C:\xampp\htdocs\tutorial\movingfiles.php on line 5

Any one knows where I am screwing up? Tried to fiddle around with it without much luck.

Thanks for looking at this.
Oct 25 '08 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
Hi.

The second parameter (the destination) should be a path to a new file, not just the directory you want that file to be in.

In other words:
Expand|Select|Wrap|Line Numbers
  1. $fileName = "file.ext";
  2. $old = "/path/to/old/" . $fileName;
  3. $new = "/path/to/new/" . $fileName;
  4.  
  5. rename($old, $new) or die("Failed to move file");
  6.  
Oct 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.