使用uploadify重命名文件名

时间:2012-06-05 11:44:25

标签: php uploadify codeigniter-2

我使用uploadify上传图片,问题是,在模型中我正在更改通过uploadify funciton.now传递的文件名。我想要在uploadify.Im警告的onUploadComplete属性中重命名的文件名该文件名但显示实际上传的文件名相同。 这是我的代码: -

        'buttonText'      : 'Select',
        'fileTypeDesc'    : 'Image Files',
        'fileTypeExts'    : '*.gif; *.jpg; *.png',
        'swf'             :'<?php echo base_url()?>resources/flash/uploadify.swf',
        'uploader'        :'<?php echo base_url().'user/upload_temp';?>',
        'width'           : 40,
        'multi'           :false,
        'onUploadComplete':function(file)
        {
            alert(file.name);
            $('.select_div').hide();
            $('.original').hide();
            $('#image1').attr('style','background-image:url("./resources/images/users/temp/'+file.name+'");background-size: 76px 76px;');
            $('.default').hide();
            $('#image2').attr('style','background-image:url("./resources/images/users/temp/'+file.name+'");background-size: 73px 66px;position:absolute; height:66px; width:73px; top:256px; left:220px;');
            $('#hidden_img_value1').attr('value',file.name)
        }
    });

在模特中我这样做: -     $ targetFolder = FCPATH。'/ resources / images / users / temp /'; //相对于根         if(!empty($ image))         {             $时间=的strtotime( “现在”);             $图像[ 'Filedata上'] [ '名称'] = $时间 'JPG'。             $ tempFile = $ image ['Filedata'] ['tmp_name'];             $ targetPath = $ targetFolder。$ image ['Filedata'] ['name'];

        // Validate the file type
        $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
        $fileParts = pathinfo($image['Filedata']['name']);

        if (in_array($fileParts['extension'],$fileTypes)) 
        {
            move_uploaded_file($tempFile,$targetPath);
            echo '1';
        } 
        else 
        {
            echo 'Invalid file type.';
        }
    }

1 个答案:

答案 0 :(得分:0)

   // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($image['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) 
    {
        move_uploaded_file($tempFile,$targetPath);


      // Set new Name
      $new_name = rand(0,10000).'.'.$fileParts['extension'];
      $new_path = $fileParts['dirname'].'/'.$new_name;

      while(file_exists($new_path))
     {
      $new_name = rand(0,10000).'.'.$fileParts['extension'];
      $new_path = $fileParts['dirname'].'/'.$new_name;
     }

      rename($targetPath,$new_path);

    } 
    else 
    {
        echo 'Invalid file type.';
    }
}