rename $filename

Discussion in 'PHP' started by MyVodaFone, May 21, 2010.

  1. #1
    Once again I need assistance.

    I'm trying to rename an Image to a urlencode($name)

    I'm working with the code below, which works fine sorting the original filename or default.jpg

    
    if (empty($image)) {
    $image = "default.jpg";
    }
    $url = "$image";
    $urlinfo = parse_url($url);
    $filename = basename($urlinfo['path']);
    $target = $image_path.'/'.$filename;
    if(file_exists($target) && ! true){
    $pathinfo = pathinfo($target);
    }
    $fh = fopen($target,'w');
    $check = fwrite($fh,file_get_contents($url));
    fclose($fh);
    
    PHP:
    If I try anything like
    
    $new_filename = urlencode($name);
    
    PHP:
    It will store the file but I'm unable to attach the file .extension .jpg or rather the DOT is ignored

    $new_filename = urlencode($name)\.jpg;
    PHP:
    Could someone show/find me an example of renaming a file with a .image extention ?
     
    MyVodaFone, May 21, 2010 IP
  2. kaleshwar

    kaleshwar Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about?
    $new_filename = urlencode($name).'.jpg';
    PHP:
     
    kaleshwar, May 21, 2010 IP
    MyVodaFone likes this.
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    That my friend does exactly what I asked, thanks...

    However it turns out, I don't need to urlencode because a name like
    Solomon+Kane+%282009%29.jpg
    Code (markup):
    wont show for some reason, where as a name like
    Solomon Kane (2009).jpg
    Code (markup):
    will strange ?

    Thanks again, I wouldn't have got the .'.jpg'; bit, well maybe not so quickly :D
     
    MyVodaFone, May 21, 2010 IP
  4. hadinih

    hadinih Member

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Maybe you can replace space with character "-" using str_replace() function :)
     
    hadinih, May 21, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    Yeh thanks I'm using that already
     
    MyVodaFone, May 21, 2010 IP
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6

    Try this
    
    $reg_ex = "[[:space:]]";
    $replace_word = "-";
    $newname = ereg_replace($reg_ex, $replace_word, $yourfilename);
    
    PHP:
     
    roopajyothi, May 21, 2010 IP