rename name file in database when upload file


I get problem to rename file upload when insert in database. can someone help me ??

If you want to insert a file into a DB you need to load the file into a binary string first, so for example the file has been uploaded:

$upload = CUploadedFile::getInstanceByName($formInputName);

$fileContents = get_file_contents($upload->tempName);

//insert $fileContents into an appropriate blob field

I’m not sure if this is what you wanted.

Edit changed getInstance to getInstanceByName.


i will try it :D

That might help you

            $attachment = CUploadedFile::getInstanceByName('CourseDownload[file]');

            //Saving the File

            $OriginalFilename = $FinalFilename = preg_replace('`[^a-z0-9-_.]`i','',$attachment->name);

            // rename file if it already exists by prefixing an incrementing number

            $FileCounter = 1;

            // loop until an available filename is found

            while (file_exists( $baseDir.'/downloads/'.$this->course_data->code.'/'.$file_type.'/'.$FinalFilename )){

              $FinalFilename = $FileCounter++.'_'.$OriginalFilename;


            // Save file