Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 28, 2012, 03:29 PM   #1
hansolo669
macrumors regular
 
Join Date: Oct 2009
php rename file problem

this is probably a simple problem, but the solution seems to be escaping me at the moment.
(edited cause my original idea was dumb)
PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/" $_FILES["file"]["name"]); 
how would i get that to (when moveing the file) also rename the file to the result of a varible?
__________________
: 13in MBP, MDD FW800, iMac g3 slot loading, iBook g4
PC: Custom build(main), IBM Intellistation p4, Dell latitude pIII

Last edited by hansolo669; Feb 28, 2012 at 04:19 PM.
hansolo669 is offline   0 Reply With Quote
Old Feb 28, 2012, 07:25 PM   #2
ppc_michael
macrumors 65816
 
ppc_michael's Avatar
 
Join Date: Apr 2005
Location: Los Angeles, CA
Send a message via AIM to ppc_michael
If I'm understanding your question, replace the $_FILES["file"]["name"] variable with the variable that contains the name you want to save it as.

So if $newname contains the new file name you want to save it as, your code would be

PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/" $newname); 
__________________
-PPC_Michael
Apple is just a company. Think for yourselves.
ppc_michael is offline   1 Reply With Quote
Old Feb 28, 2012, 09:08 PM   #3
hansolo669
Thread Starter
macrumors regular
 
Join Date: Oct 2009
Quote:
Originally Posted by ppc_michael View Post
If I'm understanding your question, replace the $_FILES["file"]["name"] variable with the variable that contains the name you want to save it as.

So if $newname contains the new file name you want to save it as, your code would be

PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/" $newname); 
yup! thanks a bunch.
my actualy code ended up as such:
PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/{$newname}$ext); 
but that was to allow for keeping the ext
__________________
: 13in MBP, MDD FW800, iMac g3 slot loading, iBook g4
PC: Custom build(main), IBM Intellistation p4, Dell latitude pIII
hansolo669 is offline   0 Reply With Quote
Old Feb 28, 2012, 10:30 PM   #4
ppc_michael
macrumors 65816
 
ppc_michael's Avatar
 
Join Date: Apr 2005
Location: Los Angeles, CA
Send a message via AIM to ppc_michael
Looks good! By the way, instead of the curly brackets you could also do:

PHP Code:
"images2012/".$newname.$ext 
But yours works just as well.
__________________
-PPC_Michael
Apple is just a company. Think for yourselves.
ppc_michael is offline   0 Reply With Quote
Old Feb 29, 2012, 03:18 AM   #5
bpaluzzi
macrumors 6502a
 
bpaluzzi's Avatar
 
Join Date: Sep 2010
Location: London
Quote:
Originally Posted by ppc_michael View Post
Looks good! By the way, instead of the curly brackets you could also do:

PHP Code:
"images2012/".$newname.$ext 
But yours works just as well.
Just for the sake of completeness -- when you're using double quotes, you don't even need the curly braces. The following will work:
PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/$newname$ext"); 
Note that this requires two conditions to be met:
1 - "simple" variable name, in the form of $variablename. E.g., not $foo['bar'] or $foo->bar
2 - double quotes. 'images2012/$newname' won't work, because single-quoted aren't run through the parser. which, incidentally, is why you should use single quotes for most strings (unless you need variable replacement, or some non-printing characters, e.g., \n) -- less processing overhead.
__________________
Mac Pro; Aluminum iMac; Mac Mini (x2); Macbook Pro; iPad; iPhone 4; TV 2 (x2); G4 Tower
bpaluzzi is offline   -1 Reply With Quote
Old Feb 29, 2012, 06:35 AM   #6
hansolo669
Thread Starter
macrumors regular
 
Join Date: Oct 2009
Quote:
Originally Posted by bpaluzzi View Post
Just for the sake of completeness -- when you're using double quotes, you don't even need the curly braces. The following will work:
PHP Code:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/$newname$ext"); 
Note that this requires two conditions to be met:
1 - "simple" variable name, in the form of $variablename. E.g., not $foo['bar'] or $foo->bar
2 - double quotes. 'images2012/$newname' won't work, because single-quoted aren't run through the parser. which, incidentally, is why you should use single quotes for most strings (unless you need variable replacement, or some non-printing characters, e.g., \n) -- less processing overhead.
learn something new everyday
thank you, incidentally i was wondering what the difference was between single and double quotes, and now i know!
__________________
: 13in MBP, MDD FW800, iMac g3 slot loading, iBook g4
PC: Custom build(main), IBM Intellistation p4, Dell latitude pIII
hansolo669 is offline   0 Reply With Quote
Old Feb 29, 2012, 06:45 AM   #7
bpaluzzi
macrumors 6502a
 
bpaluzzi's Avatar
 
Join Date: Sep 2010
Location: London
Quote:
Originally Posted by hansolo669 View Post
learn something new everyday
thank you, incidentally i was wondering what the difference was between single and double quotes, and now i know!
Glad to help!

I will say that, while my example above works fine, stylistically, I'm not a big fan of multiple variables in double quotes like that.

In other words,
I'm okay with this:
PHP Code:
echo "This is the variable value $value
But I'm not a fan of this:
PHP Code:
echo "This is two variables now $value1$value2
Again, this isn't due to it not working, it's just a style thing - makes it tough to scan the values.

For multiple variables, I generally do it similar to the way ppc_michael described.
PHP Code:
echo 'This is two variables now ' $value1 $value2 

Also, the performance increase is so small as to be imperceptible. IMO, unless you're dealing with tens of thousands of strings, and need to wrangle every absolute micro-second out of your code, it's probably best to just to stick with double-quotes. You get the variable extraction / escaping as needed, and can be consistent in your code.
__________________
Mac Pro; Aluminum iMac; Mac Mini (x2); Macbook Pro; iPad; iPhone 4; TV 2 (x2); G4 Tower
bpaluzzi is offline   0 Reply With Quote

Reply
MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Renaming files in OS X 10.7 Lion benjancewicz Mac OS X 10.7 Lion 5 Mar 31, 2012 12:19 PM
Folder problem - renaming/accessing Rory Bhoy Mac Basics and Help 1 Jul 31, 2011 12:35 PM
PHP/HTML File Editor Problems geekindisguise Web Design and Development 2 Jan 15, 2009 08:18 AM
In a php include /file.php doesn't work... MegaMan1311 Web Design and Development 9 Jul 10, 2008 02:09 PM
Finder, File Chooser, Rename Files?? chelsel Mac OS X 2 Sep 23, 2007 04:15 PM


All times are GMT -5. The time now is 09:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC