HOw to Change File name while Uploading in pHp

Discussion in 'PHP' started by ksamir2004, Apr 10, 2008.

  1. #1
    Hi All,

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

    my Question is: while uploading file i need to remove spaces & special character like *, -,/ etc.

    example: sam file.xls

    Ans should be: samfile.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
     
    ksamir2004, Apr 10, 2008 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    $new_file_name indicates the new filename
    so this can be the original filename, or you can change it to whatever you want

    i.e. $new_file_name = "samfile.xls";
     
    m0nkeymafia, Apr 10, 2008 IP
  3. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    but this code is not working.. cau you check plz..
     
    ksamir2004, Apr 10, 2008 IP
  4. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #4
    how is it not working?
     
    m0nkeymafia, Apr 10, 2008 IP
  5. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #5
    <?php
    $file_name=$HTTP_POST_FILES['ufile']['name'];
    
                  $file_ext1 = explode(".", $file_name);
                  $file_ext = count($file_ext1);
                  $file_ext = strtolower($file_ext1[$file_ext-1]);
                  
    
    $special = array('/','!','&','*',' ','-');
    
    //$new_file_name = str_replace(' ',' ',str_replace($special,'',$file_name));
    
    
    /*
    
      for random name, use this:
    	$random1 = "123456789012345678901234567890123456789012345678901234567890";
    	$random2 = "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZYZ";
    	$random2 .= "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZYZ";
    
    	$name = trim(substr(str_shuffle($random1.$random2),1,3).substr(str_shuffle($random1.$random2),1,3)); 
        $new_file_name = $name.".".$file_ext;
    
    */
    $new_file_name = "newname".".".$file_ext;
    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";
    }
    }
    ?>
    PHP:

    try this
     
    Oli3L, Apr 10, 2008 IP
  6. webrehash

    webrehash Peon

    Messages:
    163
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I use Digital File Pro to save time when creating any kind of file uploader. I'm not great with the code, so it makes it easy.
     
    webrehash, Apr 10, 2008 IP
  7. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    HI above code is not working.. its printing error...

    <?php$file_name=$HTTP_POST_FILES['ufile']['name']; $file_ext1 = explode(".", $file_name); $file_ext = count($file_ext1); $file_ext = strtolower($file_ext1[$file_ext-1]); $special = array('/','!','&','*',' ','-');//$new_file_name = str_replace(' ',' ',str_replace($special,'',$file_name));/* for random name, use this: $random1 = "123456789012345678901234567890123456789012345678901234567890"; $random2 = "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZYZ"; $random2 .= "abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZYZ"; $name = trim(substr(str_shuffle($random1.$random2),1,3).substr(str_shuffle($random1.$random2),1,3)); $new_file_name = $name.".".$file_ext;*/$new_file_name = "newname".".".$file_ext;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";}}?>


    can you check it again..
     
    ksamir2004, Apr 11, 2008 IP
  8. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    $file_name=$HTTP_POST_FILES['ufile']['name'];
    $special = array('/','!','&','*','%','-',' '); //comment by ajit
    $new_file_name =str_replace($special,'',$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";
    }
    }
    ?>
     
    singh.ajit05, Apr 11, 2008 IP
  9. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    hey ajit i am sending full code.. i am not able to sending file to server directory...

    can you check. if possible...

    <html><head>name this file</head>

    <body>
    <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form action="file_post.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
    //$path = "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/UPLOADFILE/X/"; //server pathr...
    $file_name=$HTTP_POST_FILES['ufile']['name'];
    $special = array('/','!','&','*','%','-',' '); //comment by ajit
    $new_file_name =str_replace($special,'',$file_name);
    $path= "UPLOADFILE/X/".$new_file_name;

    if(move_uploaded_file($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>
     
    ksamir2004, Apr 11, 2008 IP
  10. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #10
    can you write the error here?
     
    Oli3L, Apr 11, 2008 IP
  11. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Error means its running perfectly but thing is its not even printing file name and not uploading file to server...

    Always its Displaying Error

    Thanks
    Sam
     
    ksamir2004, Apr 11, 2008 IP
  12. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hey below is the source code.. And one thing to keep attention that in both folder
    1) UPLOADFILE 2) X
    HAVE FULE permission to upload, it means 777 in case of linux server just connect to ftp and right click on folder and change CHMOD to 777

    <?php
    //$path = "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/UPLOADFILE/X/"; //server pathr...
    $file_name=$HTTP_POST_FILES['ufile']['name'];
    $special = array('/','!','&','*','%','-',' '); //comment by ajit
    $new_file_name =str_replace($special,'',$file_name);
    $path= $_SERVER['DOCUMENT_ROOT']."/UPLOADFILE/X/".$new_file_name;

    if(move_uploaded_file($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";
    }

    ?>
     
    singh.ajit05, Apr 11, 2008 IP
  13. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You should try and check out your error logs and see if it is permission issue or path issue.

    Basically your logic is saying if move_uploaded was successful then show the info else display the word "error"

    Well why is move_uploaded failing?

    Probably due to permission error. Check the php_errors log.
     
    LittleJonSupportSite, Apr 11, 2008 IP
  14. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Hi Ajit...

    I am really sorry to say that above code is not working.. i am working on window Xp OS. even i uploaded file and i was checking print echo $path;
    it shows: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/UPLOADFILE/X/


    i don't know where is wrong... if possible plz help us.. its urgent..

    Thanks
    Sam
     
    ksamir2004, Apr 13, 2008 IP
  15. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thank you so much Ajit.. Its works fine..

    <?php
    //$path = "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/UPLOADFILE/X/"; //server pathr...
    $file_name=$HTTP_POST_FILES['ufile']['name'];//place of $HTTP_POST_FILES please use $_FILE
    $special = array('/','!','&','*','%','-',' '); //comment by ajit
    $new_file_name =str_replace($special,'',$file_name);
    $path= $_SERVER['DOCUMENT_ROOT']."/UPLOADFILE/X/".$new_file_name;

    if(move_uploaded_file($_FILES['ufile']['tmp_name'],$path)) //place of $HTTP_POST_FILES please use $_FILE
    {
    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";
    }

    ?>
     
    ksamir2004, Apr 13, 2008 IP
  16. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks Every one for great Support..

    Thanks
    Sam
     
    ksamir2004, Apr 13, 2008 IP