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
$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";
<?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
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.
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..
<?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"; } } ?>
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>
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
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"; } ?>
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.
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
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"; } ?>