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, IBM Thinkpad 390x, Dell latitude pIII, Headless Acer Netbook

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
Guest
 
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 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, IBM Thinkpad 390x, Dell latitude pIII, Headless Acer Netbook
hansolo669 is offline   0 Reply With Quote
Old Feb 28, 2012, 10:30 PM   #4
ppc_michael
Guest
 
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 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, IBM Thinkpad 390x, Dell latitude pIII, Headless Acer Netbook
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

Similar Threads
thread Thread Starter Forum Replies Last Post
How Can I Rename/Delete a File While In The "Select File To Upload" Window? JJ2Man OS X 10.8 Mountain Lion 1 Aug 6, 2013 05:38 AM
cannot rename file names of folders, files, or change case of file extensions Sossity Mac Basics and Help 8 Apr 2, 2013 05:38 AM
File rename based on file contents--possible? Brutalbrutus Mac Basics and Help 2 Jul 17, 2012 05:57 AM
How to rename a file 420benz iMac 1 Jun 7, 2012 10:32 PM

Forum Jump

All times are GMT -5. The time now is 08:52 AM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC