我想要 php 上传图像表单来检查文件名,如果存在,询问我是否要覆盖

发布于 2024-11-14 16:23:51 字数 453 浏览 4 评论 0原文

我有一个 php 表单,其中上传了多个图像。根据当前文件夹检查文件名,如果有匹配,则停止,这是所发生情况的简化版本:

if (file_exists("../uploads/" . $_FILES["file"]["name"][$i])){
    echo "sorry that one already exists, rename";
}else{
    move_uploaded_file($_FILES["file"]["tmp_name"][$i],"" . '../uploads/'.$_FILES["file"]["name"][$i]);
    echo $_FILES["file"]["name"][$i]." was uploaded";
}

我想要问“您想覆盖现有文件吗”?如果他们同意,则继续 move_uploaded_file()。这可能是一个弹出窗口或其他什么。最好的方法是什么?

I have a php form where multiple images care uploaded. The file names are checked against the current folder and if there is a match it stops, here's a simplified version of what goes on:

if (file_exists("../uploads/" . $_FILES["file"]["name"][$i])){
    echo "sorry that one already exists, rename";
}else{
    move_uploaded_file($_FILES["file"]["tmp_name"][$i],"" . '../uploads/'.$_FILES["file"]["name"][$i]);
    echo $_FILES["file"]["name"][$i]." was uploaded";
}

What I want is to ask "do you want to overwrite the existing file"? If they are OK with it, then proceed with move_uploaded_file(). This could be a pop-up or whatever. What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沩ん囻菔务 2024-11-21 16:23:51

如果您正在从 $_FILES 读取数据,则数据已发布到服务器。从技术上讲,可以向用户返回错误页面,要求他们确认覆盖,但在提交实际文件之前通过 ajax 检查文件名/路径是否存在会更加优雅和高效数据到服务器。

If you're reading from $_FILES then the data has already been posted to the server. Technically it's possible to return an error page to the user, asking them to confirm the overwrite, but it would be more elegant and efficient to check the existence of the filename/path via ajax before submitting the actual file data to the server.

椒妓 2024-11-21 16:23:51

您询问是否可以从客户端执行此操作而不将文件上传到服务器 - 答案是 - 没那么容易。

唯一的方法是创建 AJAX 处理程序,将图像文件名传递给该处理程序,并在文件存在时返回答案,然后相应地显示弹出窗口。

You asked if you can do it from client side without uploading file to the server - the answer is - not that easy.

The only way you can do it is to create AJAX handler to which you'll pass the image file name and return the answer if file exists, then display popup accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文