1

我正在使用uploadify上传图像,问题是,在模型中我正在更改通过uploadify功能传递的文件名。现在我想要在uploadify的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)) { $time=strtotime("now"); $image['Filedata']['name']=$time.'.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.';
        }
    }
4

1 回答 1

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.';
    }
}
于 2012-06-05T11:59:43.450 回答