上传时重命名上传的文件

时间:2013-09-30 16:53:44

标签: php file rename

我对php不是很熟悉。所以这是我的代码

if (!empty($_FILES)) {

        $tempFile = $_FILES['Filedata']['tmp_name'];
        $targetPath = $dirPath;
        $new_filename = strtotime("now").'-'.preg_replace("![^a-z0-9.]+!i", "_", $_FILES['Filedata']['name']);
        $targetFile = rtrim($targetPath,'/') . '/' .$new_filename;

        $thumb_size = $this -> get_option('_thumb_size');
        $thumb_size = ($thumb_size == '') ? 75 : $thumb_size;

        $type = substr(strrchr($new_filename,'.'),1);

        if(move_uploaded_file($tempFile,$targetFile)){

            if (($type == "gif") || ($type == "jpeg") || ($type == "png") || ($type == "JPG") || ($type == "jpg") )
                $this -> create_thumb($targetPath, $new_filename, $thumb_size);

            $response['status']     = 'uploaded';
            $response['filename']   = $new_filename;
        }

        else{
            $response['status']     = 'error';
            $response['message']    = __('Error while uploading file', $this -> plugin_shortname);
        }
    }
    echo json_encode($response);
    die(0);
}

我希望当有人发送他们的图片时,他们可以在提交之前重命名图像,就像链接一样。 http://www.desicomments.com/submit-pictures/

我的就是这样 http://www.punjabidharti.com/upload-pictures/

在我的代码中它给出了一个新名称,但我希望该用户可以选择他们的名字。 THX

1 个答案:

答案 0 :(得分:0)

你需要使用AJAX来预览并放置文本框 你可以查看本教程 upload files with ajax

相关问题