SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing file names while uploading

    Hi,
    I have using
    Example 38-1. File Upload Form &
    Example 38-2. Validating file uploads given in
    http://in.php.net/features.file-upload to upload file ..

    I would like to change the name while uploading . what should I do ......
    e.g. suppose the file name on the computer is abvd.jpg but while loading in the website I want to make it 123.jpg ... how to do that?

    Regards,
    kaushik

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    Romania, Iasi
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $uploadfile $uploaddir $new_file_name

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At some point you will call move_uploaded_file to save the file to the wanted directory.

    The syntax for move_uploaded_file is

    Code:
    move_uploaded_file ( string filename, string destination )
    When passing in the second parameter you can specify the filename you would like the file to be saved as.

    Example
    PHP Code:
    move_uploaded_file($_FILES['user_file']['tmp_name'],'path/123.jpg'); 
    For more details about move_uploaded_file see http://ca.php.net/manual/en/function...oaded-file.php

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    India
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Suppose an user is uploading a file abc.jpeg
    The file is already there in the upload folder($uploaddir = '/var/www/uploads/' . If the file is already present I don't want to uppload the file and display an error message. How to do that?

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick modification of code from http://ca3.php.net/manual/en/function.file-exists.php

    PHP Code:
    $newfile 'path/123.jpg';
    if (
    file_exists($newfile)) {
        echo 
    "The file $newfile exists";
    } else {
        
    move_uploaded_file($_FILES['user_file']['tmp_name'],$newfile); 

    Normally I do not prompt that the file exists, I would automatically append (x) after the file name to generate a unique filename (e.g. 123(1).jpg); I find this more user friendly.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you are also using a database to record uploaded files then you could do what I do - get the next ID number from the database and use that as the new filename. (Don't forget to add the correct file extension, like .jpg).

    An example of this can be found in the gallery at http://elfuego.siteguru.co.uk
    Ian Anderson
    www.siteguru.co.uk


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
  •