rename file before upload

I am trying to change the file name before it is stored on the server.

[code]Html sample code:

Company Name:
ClientName:

Send this file: [/code]

[php]

<?php $file_name = $_POST['company']; Define ("PATH", "/wamp/www/phpuploader/$file_name"); if(!is_dir($file_name)) { mkdir($file_name); $old_file_name = $file_name; $random_digits = rand(0000,9999); $new_file_name = $file_name.$random_digits; rename( '$old_file_name', '$new_file_name'); $uploaddir = PATH . '/'.$new_file_name; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); echo "

"; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Upload failed"; } echo "

"; echo '
';
		echo 'Here is some more debugging info:';
		print_r($_FILES);
		print "
"; } ?>

[/php]

I am having problem with the rename. It renames the file but it keeps the original name too. So if I upload a file name joey.doc, it changes to random# = 4567, company name = whatever, file name = joey.doc output = 4567whateverjoey.doc. What I am looking for is for the file name to change to the random number append the company to it. Later I will write code to create company name directory store all file from that company in their directory. Store the location tag in xml then pull up a table with to reference. the information. Plus I use the client name input box to decode what file go with what.

If writing xml is like asp.net C# I should have no problem

Are you trying to change:

somefile.doc

To:

somefileRANDOMNUMBER.doc

If so, you could split the string and build it back together:

[php]<?php

$filename = ‘testfile.doc’;
$suffix = 12345;

$parts = explode(’.’, $filename);

$parts[count($parts) - 2] = $parts[count($parts) - 2] . $suffix;

$filename = implode(’.’, $parts);

echo $filename;[/php]

Try it out: http://codepad.org/94xbNFBU

no I am trying to somefile.doc

To:

 newnameRANDOMNUMBER.doc
hi do the below thing for rename file. [php] $fname = 'sr.doc'; //add time-stamp or any suffix you want. i am using time-stamp because it's always unique. $curtime = time();

$data = explode(’.’, $fname);
//new name of your doc file
$newfilename = $data[0].$curtime.’.’.$data[1];
echo $newfilename

[/php]
i hope this will resolve your problem.
Replay Your Feedback
SR

I will let you know today.

Thanks,

worked thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service