dongshicuo4844 2013-05-13 21:00
浏览 111
已采纳

move_uploaded_file:不允许使用tmp_name? [关闭]

Question:

When you use the move_uploaded_file function, can you keep the name that was created in the tmp_name? or must you always change the name to something else?

If you can keep the name of the file that you are moving as the tmp_name, how must you pass in the argument? I tried the following code, and I can't seem to get it to work.

Please see the code below:

if(isset($_POST['submit'])){
    $tmp_file = $_FILES['file_upload']['tmp_name'];
    $target_file = basename($_FILES['file_upload']['name']);
    $upload_dir = "uploads";

    if(move_uploaded_file($tmp_file, $upload_dir."/".$tmp_file)){
        $message = "File uploaded successfully.";
    }else{
        $error = $_FILES['file_upload']['error'];
        $message = $upload_errors[$error];
    }
}

When I pass the second argument in as $upload_dir."/".$target_file , there doesn't seem to be any issues, but when I pass it as $upload_dir."/".$tmp_file as shown in the codes above, it keeps giving me the following error:

SCREAM: Error suppression ignored for

Warning: move_uploaded_file(uploads/C:\wamp\tmp\php1EA0.tmp): failed to open stream: Invalid argument in C:\wamp\www\bb2\uploads.php on line 26
  • 写回答

3条回答 默认 最新

  • duanrongpai9556 2013-05-13 21:06
    关注

    tmp_name is not a file name, it's a full path to the file.

    If you just want a random filename, I suggest code like this:

    $dest = $_FILES['file_upload']['name'];
    while (file_exists($dest)) {
      $dest = $upload_dir . '/' . substr(sha1(rand()), 0, 10);
    }
    
    move_uploaded_file($_FILES['file_upload']['name'], $dest);
    

    rand() generates a random big number. sha1() uses that as the seed to create an even bigger random hex string, we don't want something that big so just grab the first 10 characters or so.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码