Jump to content

HOw to Change File name while Uploading in pHp


ksamir2004

Recommended Posts

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'];

$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";

}

}

?>

</body>

</html>

 

Thanks

in advance..

 

Please mail this code after correction..

 

Thanks

Sam

 

main thing is i need to remove blank space form file name

 

Example: file x uploaded.xls

 

Ans should be: filexuploaded.xls

 

this code gived file extension

 

<?php

//This function separates the extension from the rest of the file name and returns it

function findexts ($filename)

{

$filename = strtolower($filename) ;

$exts = split("[/\\.]", $filename) ;

$n = count($exts)-1;

$exts = $exts[$n];

return $exts;

}

 

//This applies the function to our file

$ext = findexts ($_FILES['uploaded']['name']) ;

---------------------------------------------

 

 

i need complete solution ..

 

plz help us...

 

Thanks

sam

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.