我是php新手并试图将文件上传到指定目录。 但是当我检查下面给出的条件时:
if(file_exists($target_dir))//check if file already exists in uploads folder
{
echo "sorry! file already exists";
$uploadOK=0;
}
它显示“抱歉!文件已存在”;。 但我从C:\ mypictures \中选择我的图像文件,目标目录的路径是C:\ xampp \ htdocs \ uploads。 我不知道为什么会这样。 如果有人能澄清这一点会很棒。
这是我的upload.php文件的完整代码:
<?php
$target_dir="C:/xampp/htdocs/uploads/";
$target_file=$target_dir.basename($_FILES["fileToUpload"]["name"]);
$uploadOK=1;
$ImageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"]))
$check=getimagesize($_FILES["fileToUpload"]["tmp_name"]);//check if a file is an image or not
if($check == true)
echo "file is an image";
else
{
echo "file is not an image";
$uploadOK=0;
}
if(file_exists($target_dir))//check if file already exists in uploads folder
{
echo "sorry! file already exists";
$uploadOK=0;
} //doubt
if(filesize($target_file>500000))//check if the file is too large
{
echo "file too large";
$uploadOK=0;
}
if($ImageFileType!="jpg" && $ImageFileType!="png" && $ImageFileType!="jpeg" && $ImageFileType!="png")//check file type
{
echo "file type not supported";
$uploadOK=0;
}
if($uploadOK==0)//checking for any errors
{
echo "some error occured.";
}
else{
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo "file uploaded";
}
else{
echo "file not uploaded....there was some error.";
}
}
?>
答案 0 :(得分:0)
因为您正在检查目录而不是文件。将您的代码更改为
if(file_exists($target_file))//check if file already exists in uploads folder
{
echo "sorry! file already exists";
$uploadOK=0;
} //do