Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    change file name upon upload

    I'd like to alter this code so that the photo uploaded is renamed with the string value of $f_userkey so it comes out like myusername.gif (or.jpg) I'd also like to load a string indicating the file type (.gif or .jpg) into mysql as $picsuffix so i can call it later as
    $photoname = /path/dir/.$f_userkey.$picsuffix

    Is that retarded? If not... how do i do it?

    PHP Code:
    if (is_uploaded_file($HTTP_POST_FILES['f_photo']['tmp_name'])) {
     if (
    $HTTP_POST_FILES['f_photo']['size']>$max_size) { 
     
    error("The file is too big"); 
     }
     if ((
    $HTTP_POST_FILES['f_photo']['type']=="image/gif") || ($HTTP_POST_FILES['f_photo']['type']=="image/pjpeg") || ($HTTP_POST_FILES['f_photo']['type']=="image/jpeg")) {

      
    $res copy($HTTP_POST_FILES['f_photo']['tmp_name'], $imagepath .
      
    $HTTP_POST_FILES['f_photo']['name']);

      if (!
    $res) {
      
    error("upload failed!"); 
      }
       
     } 
    else { 
    error("Wrong file type. Please use JPG or GIF");
     }



  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use something like this:

    move_uploaded_file($HTTP_POST_FILES['f_photo']['tmp_name'], '/path/dir/' . $f_userkey . $picsuffix)

    That will do all the moving and renaming for you.

    (I dont know why the forum put s line break in "tmp", it shouldn't be there).
    Last edited by Dylan Leblanc; 02-07-2003 at 05:56 AM.


  •  

    Posting Permissions

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