View Full Version : Changing File Names
dudeman0501
03-08-2005, 08:48 PM
Is there anyway that I can change the name of a file on my server using PHP? I've searched the manual, but I didn't find anything I thought would help.
Thanks.
dudeman0501
03-08-2005, 09:10 PM
Never mind. I found out how to do it.
I need to learn to search using the right terms... :rolleyes:
I think you're looking for the function bool rename(string oldname, string newname[, resource context]) (http://php.net/rename)
even easier than searching -- type in php.net/<what you think the function should be named> and if it doesn't work you have a link to the manual or possibly the function you're looking for.
dudeman0501
03-08-2005, 09:45 PM
Well, I thought I had it working...
Here's the deal:
When one of my users uploads a file, my script uploads the file, opens a .txt file to get a number, adds 1 to that number, and writes the new number back to the .txt file.
I am then wanting it to rename the uploaded file to that number. Here's my rename function:
rename ("/home/virtual/site111/fst/var/www/html/images/parts/$_FILES['userfile']['name']", "/home/virtual/site111/fst/var/www/html/images/parts/$contents.jpg");
When I run the script, I get this error "Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /home/virtual/site111/fst/var/www/html/siteadmin/add.php on line 42"
Any ideas?
mordred
03-08-2005, 10:17 PM
Remove the single quotes in your first path.
dudeman0501
03-08-2005, 10:36 PM
Remove the single quotes in your first path.
Alright, I changed it to this: rename ("/home/virtual/site111/fst/var/www/html/images/parts/$_FILES[userfile][name]", "/home/virtual/site111/fst/var/www/html/images/parts/$contents.jpg");
And I got this error: "Warning: rename(/home/virtual/site111/fst/var/www/html/images/parts/Array[name],/home/virtual/site111/fst/var/www/html/images/parts/49.jpg): No such file or directory in /home/virtual/site111/fst/var/www/html/siteadmin/add.php on line 42".
I feel like I'm missing something that blindingly obvious...
Hmmm... isn't it standard practice to copy the temporary uploaded file rather than renaming it? I was somewhat recently involved in a thread with upload issues, I'll post a link when I find it.
*doh* it was a different topic, I was remembering another one from even longer ago. After searching google for "PHP Uploading", I found what should be a useful page for how to upload files using move_uploaded_file() (http://www.php.net/manual/en/function.move-uploaded-file.php), here: http://www.netspade.com/articles/php/uploading.xml
dudeman0501
03-08-2005, 10:59 PM
Hmmm... isn't it standard practice to copy the temporary uploaded file rather than renaming it? I was somewhat recently involved in a thread with upload issues, I'll post a link when I find it.
*doh* it was a different topic, I was remembering another one from even longer ago. After searching google for "PHP Uploading", I found what should be a useful page for how to upload files using move_uploaded_file() (http://www.php.net/manual/en/function.move-uploaded-file.php), here: http://www.netspade.com/articles/php/uploading.xml
I'm not having problems uploading, just renaming the file. The code on that page is exactly what I'm using to upload the file.
mordred
03-08-2005, 11:04 PM
Argh, PHP string parsing madness... try it with concatenation instead, that should dereference the multi-dimensional array correctly. Or so I tink.
"/home/virtual/site111/fst/var/www/html/images/parts/" . $test['userfile']['name']
dudeman0501
03-08-2005, 11:16 PM
Argh, PHP string parsing madness... try it with concatenation instead, that should dereference the multi-dimensional array correctly. Or so I tink.
"/home/virtual/site111/fst/var/www/html/images/parts/" . $test['userfile']['name']
I hadn't ever thought about that. It fixed it.
Thanks a buch!
dudeman0501
03-08-2005, 11:58 PM
1 more question...
Why would I be getting a parse error on the very last line of my code? The only thing in the line is "?>".
:confused:
dudeman0501
03-09-2005, 01:14 AM
1 more question...
Why would I be getting a parse error on the very last line of my code? The only thing in the line is "?>".
:confused:
Anyone?
Is it saying it expected (or didn't expect) a ", ', }, ), or ;? If so, look for places where there are either too many or too few of them. If this is the syntax error, it should occur fairly close to the bottom. It's hard to say without any code or an error message though.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.