PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,109 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,024 people online right now. Registration is fast and FREE... Join Now!




rename file fails

 

rename file fails, PHP Cookbook rename recipe doesn't work

fortesque

7 Nov, 2009 - 09:02 AM
Post #1

New D.I.C Head
*

Joined: 2 Jun, 2009
Posts: 10

Hi!
I'm tring to use rename file code from PHP Cookbook. Here's the PHP cookbook code.
CODE

Example 24-18. Moving a file
<?php
$old = ' /tmp/today. txt';
$new = ' /tmp/tomorrow. txt';
rename($old, $new) or die("couldn' t move $old to $new: $php_errormsg");
?>


and here's how I have tried to use this code snippet.
CODE



<?php

$old =('http://www.headacheanalysis.com/reports/saved_test.html');

$new = ('http://www.headacheanalysis.com/reports/saved_test_2.html');

rename($old, $new) or die("couldn' t move $old to $new: $php_errormsg");

?>


However, I get the following error message. Can you tell me what I'm doing wrong?

"couldn' t move http://www.headacheanalysis.com/reports/saved_test.html to http://www.headacheanalysis.com/reports/saved_test_2.html: "
Many thanks
John B

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Rename File Fails

7 Nov, 2009 - 09:19 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
HTTP URLs are not supported in rename(). Also, no parenthesis in the old and new strings. Why do you have them there? You have to use file paths on the given system. Notice how the cookbook example uses a relational path, not a full URL. If www.headacheanalysis.com is the host you are running your script on, make sure the script is in the "reports" folder and use...

CODE


<?php

$old ='saved_test.html';

$new = 'saved_test_2.html';

rename($old, $new) or die("couldn' t move $old to $new: $php_errormsg");

?>


Or if you want you can have the PHP in the root folder and then in that case you would use...


CODE


<?php

$old ='/reports/saved_test.html';

$new = '/reports/saved_test_2.html';

rename($old, $new) or die("couldn' t move $old to $new: $php_errormsg");

?>


Hope this helps! smile.gif
User is offlineProfile CardPM
+Quote Post

fortesque

RE: Rename File Fails

7 Nov, 2009 - 11:18 AM
Post #3

New D.I.C Head
*

Joined: 2 Jun, 2009
Posts: 10

Martyr2
thanks for the help. Got the rename script to work by including it in the /reports/directory as you instructed. Didn't get the php script to work when I place it in the root directory. Strange? Did change the path for root directory placement to include /reports/. Can't understand why not.
JWB. smile.gif
Here's the code that wouldn't work when placed in root direcory.
CODE

<?php



$old ='/reports/saved_test.html';

$new = '/reports/saved_test_2.html';

rename($old, $new) or die("couldn' t move $old to $new: $php_errormsg");

?>

User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Rename File Fails

7 Nov, 2009 - 11:20 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Glad things are working for you. I am assuming the reports folder is a folder in the web root directory? If not, you have to specify every folder down the line until the reports folder. It would need to be the full path from web root to the actual file. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:51PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month