微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

上载和覆盖已经存在的文件

如何解决上载和覆盖已经存在的文件

我已经创建了一个设置,可以上传一个基于产品ID命名该文件文件,借助此处的一些提交,我设法使其正常运行。有时候,上传内容并不想覆盖我的文件。我的脚本中是否存在任何明显的错误

$dir = DIR . '/images/productthumbs/';
        if (!is_dir($dir))
        {
            die(construct_phrase($vbphrase['invalid_directory'],$dir));
        }
        $ext = substr($file['name'],strrpos($file['name'],'.'));
        $new_file = $dir . basename('thumbnail_product_' . $id . $ext);
        print_dots_start($vbphrase['please_wait_while_upload']);
        if(file_exists($new_file)) unlink($new_file);{
          move_uploaded_file($new_file);
          if (!move_uploaded_file($file['tmp_name'],$new_file))
          {
            print_dots_stop();
            die(construct_phrase($vbphrase['error_upload'],$file['error']));
          }
        }

解决方法

如果上传与ex文件同名的文件,则ex文件将被删除,新文件将被覆盖。 或者,在上传新文件之前,您可以删除ex文件并上传新文件。 您的某些代码是错误的,您应该编写真实的代码。据我了解,您必须使用:

更改代码
<?php 
   $dir = DIR . '/images/productthumbs/';
   if (!is_dir($dir))
   {
      die(construct_phrase($vbphrase['invalid_directory'],$dir));
   }
   $ext = substr($file['name'],strrpos($file['name'],'.'));
   $new_file = $dir . basename('thumbnail_product_' . $id . $ext);
   print_dots_start($vbphrase['please_wait_while_upload']);
   $ex_file="same path of ex file"; // you have to remove ex file
   if(file_exists($ex_file))
  {
     unlink($ex_file); // with this code you remove ex file
  if (!move_uploaded_file($file['tmp_name'],$new_file))
  {
    print_dots_stop();
    die(construct_phrase($vbphrase['error_upload'],$file['error']));
  }

} ?>

请注意$ ex_file。我不知道哪个变量具有当前的旧文件。我将其重命名为一些名称,以便您清楚理解。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。