我想要 php 上传图像表单来检查文件名,如果存在,询问我是否要覆盖
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在从
$_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.您询问是否可以从客户端执行此操作而不将文件上传到服务器 - 答案是 - 没那么容易。
唯一的方法是创建 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.