doujiao3016 2016-02-01 12:35
浏览 107
已采纳

使用tmp名称保存上传的文件

Is it a good practice to keep an uploaded file with it's tmp_name? Check the following example:

$short_tmp_name = substr($_FILES['tmp_name'], -6)
move_uploaded_file($_FILES['file'], "$uploads_dir/$short_tmp_name");

When handling file uploading like in above example:

  1. Will name collisions happen?
  2. Does this practice reveal any security issues?

EDIT:

I've clarified the question(modified the code example).

  • 写回答

1条回答 默认 最新

  • dopt85756 2016-02-01 12:48
    关注

    there is no issue as such, but to be on safer side and handle error, do something like shown below. NOTE : Just a sample code, add more error/other checks and handling based on your need

    $error = '';
    
    if ($_FILES["file"]["size"] == 0) {
        $error = 'Uploading failed';
    } 
    else if ($_FILES["file"]["size"] > MAX_UPLOAD_FILE_SIZE) {
        $error = 'File size exceeds ' . MAX_UPLOAD_FILE_SIZE_MB;
    } 
    else if ($_FILES["file"]["error"] > 0) {
        $error = 'Error while uploading';
    }
    
    if(!$error) {
        $file_name = $_FILES["file"]["name"];
    
        if (file_exists(DESTINATION . $file_name))  {
            $path_parts = pathinfo($dstFile . $file_name);
            $file_name = $path_parts['filename'] . '-' . time() . '.' . $path_parts['extension'];
        }
    
        $dstFile = DESTINATION . $file_name;
    
        if (move_uploaded_file($_FILES["file"]["tmp_name"],$dstFile)) {
            //its done
        }
        else {
            $error = 'Unexpected system error';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器