douwei1930 2017-08-23 06:36
浏览 63

将文件上载到指定目录时出错

i am new with php and was trying to upload a file to a specified directory. but when I check the condition given below:

     if(file_exists($target_dir))//check if file already exists in uploads folder
    {
    echo "sorry! file already exists";
    $uploadOK=0;
  }

it shows that "sorry! file already exists";. but i am picking my imagefile from C:\mypictures\ and path to my target directory is C:\xampp\htdocs\uploads. i am having no idea why is this happening. It would be great if anyone could clarify this.

here is the full code of my upload.php file:

<?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.";
    }
}  


?>
  • 写回答

1条回答 默认 最新

  • duanli8391 2017-08-23 06:38
    关注

    Because you are checking directory not file. change your code to

    if(file_exists($target_file))//check if file already exists in uploads folder
      {
        echo "sorry! file already exists";
        $uploadOK=0;
      } //do
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)