上载时,同名图像在php中不显示错误
帮助我获取同名图像上的错误上载时,同名图像在php中不显示错误,php,Php,帮助我获取同名图像上的错误 <form action="img.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" value=""/> <input type="submit" value="upload"/> </f
<form action="img.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" value=""/>
<input type="submit" value="upload"/>
</form>
文件名:
我的行动页面是
<?php
$image_name = $_FILES["file"]["name"];
$first_char = substr($image_name, 0, 1);
if (!file_exists($first_char)) {
mkdir($first_char);
}
$file = ($_FILES['file']['name']);
$uploadfile = $first_char;
if(file_exists($_FILES['file']['tmp_name'])) {
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile."/".$file);
echo "GOOD";
} else {
echo "ERROR";
}
?>
当我提交任何图像时,它会显示GOOD
消息。
另一方面,它运行良好您的代码正在进行错误检查
if(file_exists($_FILES['file']['tmp_name']))
您需要检查刚刚上传的文件是否存在,您必须执行以下操作
if(!file_exists($uploadfile."/".$file))
这将检查是否存在同名文件,如果不存在,则可以将新文件移入,如果存在,则可以显示错误。您正在检查移动图像的临时目录。但是如果已经有同名文件,我想你应该检查一下目的地
尝试将
如果(文件存在($\u FILES['file']['tmp\u name'])
更改为如果(文件存在($uploadfile./“$file))
您只是做了错误的检查:$\u FILES['file']['tmp\u name']
而不是$uploadfile./“$file
。