开发者

I want php upload image form to check filename and, if it exists, ask if I want to overwrite

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜