Results 1 to 2 of 2

Thread: How to Rename file while uploading in PHP

  1. #1
    Join Date
    Mar 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to Rename file while uploading in PHP

    Hi All,

    I am posting code to you...
    please help us..

    my Question is: while uploading file i need to rename file name(special character should not be there like *, ? ,/ ,-)
    example: sam-file / 21-11-08.xls

    Ans should be: samfile221108.xls

    So i am pasting code:

    --------------------------------------------
    File name is : upload_rename.php
    -----------------------------------------------
    <html><head>name this file</head>

    <body>
    <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form action="upload_rename.php " method="post" enctype="multipart/form-data" name="form1" id="form1">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td><strong>Single File Upload </strong></td>
    </tr>
    <tr>
    <td>Select file
    <input name="ufile" type="file" id="ufile" size="50" /></td>
    </tr>
    <tr>
    <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>

    <?php
    $file_name=$HTTP_POST_FILES['ufile']['name'];

    $special = array('/','!','&','*',' ','-');

    $new_file_name = str_replace(' ',' ',str_replace($special,'',$file_name));
    echo '['.$new_file_name.']';

    $path= "uploads/File/".$new_file_name;

    if($ufile !=none)
    {
    if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
    {
    echo "Successful<BR/>";
    echo "File Name :".$new_file_name."<BR/>";
    echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
    echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
    }
    else
    {
    echo "Error";
    }
    }
    ?>
    </body>
    </html>

    Thanks
    in advance..

    Please mail this code after correction..

    Thanks
    Sam

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    The only thing I could see wrong with this code is this line:

    Code:
    $new_file_name = str_replace(' ',' ',str_replace($special,'',$file_name));
    it should be like so:

    Code:
    $new_file_name = str_replace($special,'',$file_name);
    If you are having problems with it, please post either a link to the problem page or post what exactly is giving you issues with it.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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
  •