php rename file problem

Discussion in 'Web Design and Development' started by hansolo669, Feb 28, 2012.

  1. hansolo669, Feb 28, 2012
    Last edited: Feb 28, 2012

    hansolo669 macrumors regular

    Joined:
    Oct 5, 2009
    #1
    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?
     
  2. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #2
    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);
     
  3. hansolo669 thread starter macrumors regular

    Joined:
    Oct 5, 2009
    #3
    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
     
  4. ppc_michael Guest

    ppc_michael

    Joined:
    Apr 26, 2005
    Location:
    Los Angeles, CA
    #4
    Looks good! By the way, instead of the curly brackets you could also do:

    PHP:
    "images2012/".$newname.$ext
    But yours works just as well.
     
  5. bpaluzzi macrumors 6502a

    bpaluzzi

    Joined:
    Sep 2, 2010
    Location:
    London
    #5
    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.
     
  6. hansolo669 thread starter macrumors regular

    Joined:
    Oct 5, 2009
    #6
    learn something new everyday :D
    thank you, incidentally i was wondering what the difference was between single and double quotes, and now i know!
     
  7. bpaluzzi macrumors 6502a

    bpaluzzi

    Joined:
    Sep 2, 2010
    Location:
    London
    #7
    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.
     

Share This Page