Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hansolo669

macrumors regular
Original poster
Oct 5, 2009
201
0
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:
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?
 
Last edited:

ppc_michael

Guest
Apr 26, 2005
1,498
2
Los Angeles, CA
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:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/" . $newname);
 

hansolo669

macrumors regular
Original poster
Oct 5, 2009
201
0
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:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/" . $newname);
yup! thanks a bunch.
my actualy code ended up as such:
PHP:
move_uploaded_file($_FILES["file"]["tmp_name"], "images2012/{$newname}" . $ext);
but that was to allow for keeping the ext
 

bpaluzzi

macrumors 6502a
Sep 2, 2010
918
1
London
Looks good! By the way, instead of the curly brackets you could also do:

PHP:
"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:
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.
 

hansolo669

macrumors regular
Original poster
Oct 5, 2009
201
0
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:
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 :D
thank you, incidentally i was wondering what the difference was between single and double quotes, and now i know!
 

bpaluzzi

macrumors 6502a
Sep 2, 2010
918
1
London
learn something new everyday :D
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:
echo "This is the variable value $value"

But I'm not a fan of this:
PHP:
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:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.