Change filename after/during upload.

Discussion in 'PHP' started by sittsait, Nov 8, 2008.

  1. #1
    Hi,
    i was wondering if its possible to change these characters: "ö,ä,ü,õ" in a filename, if it contains any of em and then proceed with move_uploaded_file function.
    So if i upload õpilased.php
    It would be saved as 6pilased.php

    Thank you very much ;)
     
    sittsait, Nov 8, 2008 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    use urlencode()
     
    Lordo, Nov 8, 2008 IP
  3. sittsait

    sittsait Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, i looked into urlencode function, but its way too complicated for me. Im still learning. Then i tried str_replace function. Code in red is the one i tried to use in this upload. I tried to remove spaces to see if this even works. It didnt :eek:

    
    <?php
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
      $filename = basename($_FILES['uploaded_file']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);
      if ($_FILES["uploaded_file"]["size"] < 100000000) {
          $newname = dirname(__FILE__).'/6pilased/test/'.$filename;
    	  [COLOR="Red"][B]str_replace(' ', '_', $filename);[/B][/COLOR]
          if (!file_exists($newname)) {
            if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
               echo "Fail on edukalt laetud!";
            }
          } else {
            echo "Antud failinimi ".$_FILES["uploaded_file"]["name"]." on juba kasutusel!";
          }
      }
    } else {
     echo "Faili ei saadetud!";
    }
    ?>
    
    Code (markup):
     
    sittsait, Nov 8, 2008 IP
  4. sittsait

    sittsait Guest

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I LOVE MYSELF :D

    
    [B][COLOR="DarkRed"]$filename = str_replace(array('ö', 'ä', 'ü', 'õ', ' '), 
      array('o', '2', 'y', '6', '_'), $_FILES["uploaded_file"]["name"]);[/COLOR][/B]
    
    Code (markup):
     
    sittsait, Nov 8, 2008 IP
  5. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Good that you found the solution yourself :)
    You can use str_ireplace to be covered for hyphens.

    Cheers
     
    wing, Nov 8, 2008 IP
  6. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i think you should just change the filename entirely, to avoid file overwriting
     
    shineDarkly, Nov 8, 2008 IP