SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Renaming a file after upload

    In another forum posting I requested help with code, to add to a script, that takes a file, after it's uploaded, and renames it with the logged-in users name (and keeps the same file extension) and moves it to a directory folder.

    The code below does that successfully when added to the upload script.

    I would like to modify this code so it renames uploaded files by combining the uploaded file name with the user name.

    For example, Joe, the logged-in users name, uploads a file named MyLife.txt would be renamed MyLife Joe.txt.

    Because this code (below) works successfully presently, I'd rather have someone help me modify it rather than provide all new code. Thank you. Any help would be greatly appreciated.

    }
    function upload_file($upload_directory, $upload_uri) {global $account;
    $file_name = $_FILES["userfile"]["name"];
    $file_name = str_replace(" ","_",$file_name);
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);
    $file_path = $upload_directory.$account->get_user_name().'.'.$ext;
    $temporary = $_FILES["userfile"]["tmp_name"];

    $result = move_uploaded_file($temporary, $file_path);
    if(!chmod($file_path,0777))
    $message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
    else $message = ($result)?"File has been uploaded." : "An error has occurred.";
    return $message;
    }

    p.s. the script uses $account->get_user_name(); to get the logged-in user name.

  2. #2
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the rename() function.
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change this line
    PHP Code:
    $file_path $upload_directory.$account->get_user_name().'.'.$ext
    To this
    PHP Code:
    $file_name basename($file_name'.' $ext);
    $file_path $upload_directory$file_name.'_'$account->get_user_name().'.'.$ext
    I've added an underscore between the file name and username because spaces aren't a good idea on the web/linux file systems.

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    calif
    Posts
    743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you

    Thank you that works great.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •