gpt4 book ai didi

php - 当我的文件 uploader 中没有选择文件时,如何收到错误消息?

转载 作者:行者123 更新时间:2023-11-28 05:26:43 26 4
gpt4 key购买 nike

这是我的文件 uploader :

    <form action="done.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>

这是现在发生的事情:

如果我没有选择任何文件并单击“提交”,我将被重定向到 done.php。

我需要什么:

如果没有选择文件并单击提交,我想留在我的网站上并收到错误消息“未选择文件”。

完成.php:

    if(isset($_FILES['file'])) {

$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);

$filename = $target_file;

if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}

我现在按照 Florian 的建议更新了我的代码:

完成.php:

    if(isset($_FILES['file'])) {

$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);

$filename = $target_file;

if (file_exists($target_file)) {
echo "file already existes";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
}
}
else {
echo 'No file selected. <a href="upload.html">Back to upload form</a>';
}

现在发生的情况是,当我没有选择任何文件时,我收到错误消息:“文件已存在”。我不明白为什么。

最佳答案

扩展你的 done.php 如下:

if(!file_exists($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
$filename = $target_file;

move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
} else {
echo 'No file selected. <a href="./upload.html">Back to upload form</a>';
// maybe you want to include the upload form here or do something else
}

关于php - 当我的文件 uploader 中没有选择文件时,如何收到错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31584808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com