开发者

change file name in mysql -joomla and store has a unique name in database?

hi friends this is my code for save -> for uploading a image only if the insert is succesfull based on id

but have a problem that the file has moved to the upload folder but has not been updated in database how do update the uploaded image in database and chane the name of uploaded image so that its unique

like mypic.jpg becomes mypic90.jpg after being uploded and then u开发者_如何学运维pdated to the datbase with the user id. i hope my question helps others like me ?

function save() {

        $greeting = JRequest::get ( 'POST' );
        $model = & $this->getModel ( 'greetings' );

        $id = $model->saveGreeting ( $greeting );

        if( !empty($id)){
            $file = JRequest::getVar ( 'greeting_pic', null, 'files', 'array' );

            jimport ( 'joomla.filesystem.file' );
            $filename = JFile::makeSafe ( $file ['name'] );

            //Set up the source and destination of the file
            $src = $file ['tmp_name'];

            //$dest = JPATH_ROOT . BI_IMAGE_BASE . "myfile.jpg<br>";
            $dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;

            JFile::upload ( $src, $dest );

            // update the field greeting_pic //
            //$result = $model->updateGreetingPic($filename, $id);
        }

        $redirectTo = JRoute::_ ( 'index.php?option=' . JRequest::getVar ( 'option' ) . '&task=display' );
        $this->setRedirect ( $redirectTo, 'Greeting Saved!' );
    }


Rename the uploaded file to the unique id of the greeting, keeping the file extension. Thus you'll avoid conflicts of the filenames:

$newFileName = $id . '.' .  strtolower( JFile::getExt( $fileName));
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $newFileName;
$result = $model->updateGreetingPic($newFileName, $id);

There will be files like 90.jpg 91.png 92.gif etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜