Forum Moderators: coopster

Message Too Old, No Replies

replace and rename invalid characters and file

replace and rename invalid characters and file

         

drooh

10:41 pm on Mar 12, 2008 (gmt 0)

10+ Year Member



I am writing a function that will examine a filename from an upload and then replace and invalid characters and replace them as well as actually renaming the file on the server. right now ive got it to replace to invalid characterse but Im having trouble getting it to rename the file. here is my code:

$upload_directory = "uploads/";

function replace_invalid_chars($file_name){
$invalid_chars = array(' ', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '=', '+', '[', ']', '{', '}', '\\', '¦', ':', ';', '"', '\'', ',', '<', '>', '/', '?');
$corrected_chars = array('_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_');
$newname = str_replace($invalid_chars, $corrected_chars, $file_name);
return $newname;
}


then i call the function like this

$uploaded_file1_rename = replace_invalid_chars($uploaded_file1);
rename($upload_directory.$uploaded_file1,$upload_directory.$uploaded_file1_rename);

id like for the function replace_invalid_chars to take care of the actual renaming instead of having to call the rename function on the next line of code.

coopster

1:51 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Use the return value of the function in your concatenation statement. You can do this during the move_uploaded file process rather than call the rename function later.

drooh

5:07 pm on Mar 13, 2008 (gmt 0)

10+ Year Member



can you give me an example?

coopster

6:19 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



rename($upload_directory.$uploaded_file1,$upload_directory.replace_invalid_chars($uploaded_file1));