为什么我的isset(($ _ FILES [])函数在PHP中不起作用?

时间:2015-12-27 12:43:18

标签: php

我创建了一个html表单,用户可以上传自己的个人资料图片,

<form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="profilepic"/> <br></br>
  <input type="submit" name="uploadpic" value="Upload Image" />
</form>

然后制作了一个PHP代码,将上传的图像存储到一个随机生成名称

的新文件夹中
if (isset($_FILES['profilepic']))  {
    echo "set!";
   if (((@$_FILES["profilepic"]["type"]=="image/jpeg") || (@$_FILES["profilepic"]["type"]=="image/png") || (@$_FILES["profilepic"]["type"]=="image/gif"))&&(@$_FILES["profilepic"]["size"] < 1048576)) //1 Megabyte
  {
   $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   $rand_dir_name = substr(str_shuffle($chars), 0, 15);
   mkdir("userdata/profile_pics/$rand_dir_name");

   if (file_exists("userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"]))
   {
    echo @$_FILES["profilepic"]["name"]." Already exists";
   }
   else
   {
    move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/profile_pics/$rand_dir_name/".$_FILES["profilepic"]["name"]);
    //echo "Uploaded and stored in: userdata/profile_pics/$rand_dir_name/".@$_FILES["profilepic"]["name"];
    $profile_pic_name = @$_FILES["profilepic"]["name"];
    $profile_pic_query = mysql_query("UPDATE users SET profile_pic='$rand_dir_name/$profile_pic_name' WHERE username='$user'");
    header("Location: info.php");

   }
  }
  else
  {
      echo "Invalid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
  }
  echo "set!!";
  }
  else if (!isset($_FILES['profilepic'])){
  echo "not set!";}

问题是,在我上传照片之后,它永远不会进入isset()条件,它总是返回false并且我的代码都没有被执行。

任何猜测为什么?

1 个答案:

答案 0 :(得分:0)

这似乎没问题,希望你能利用它。

<?php
    if ( isset( $_FILES['profilepic'] ) )  {

        $temp=$_FILES['profilepic']['tmp_name'];
        $type=$_FILES['profilepic']['type'];
        $size=$_FILES['profilepic']['size'];
        $name=$_FILES['profilepic']['name'];

        $max=pow(1024,2);
        $allowed=array( 'image/jpeg', 'image/png', 'image/gif' );

        if( in_array( $type, $allowed ) && $size < $max ){

            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $dir = substr( md5( str_shuffle( $chars ) . time() ), 0, 15 );

            mkdir( "userdata/profile_pics/{$dir}" );
            $imgpath="userdata/profile_pics/{$dir}/{$name}";

            if ( file_exists( $imgpath ) ){
                echo $name.' already exists!';
                clearstatcache();

            } else {

                $status=move_uploaded_file( $temp,$imgpath );
                if( $status ){
                    $res = mysql_query( "UPDATE `users` SET `profile_pic`='$imgpath' WHERE `username`='$user';");
                }
                header("Location: info.php?status=$status");

            }
        } else {
            echo "Invalid File! Your image must be no larger than 1MB and it must be either a .jpg, .jpeg, .png or .gif";
        }
    }
?>
<html>
    <head>
        <title>upload</title>
    </head>
    <body>
        <form action="" method="post" enctype="multipart/form-data">
          <input type="file" name="profilepic"/> <br></br>
          <input type="submit" name="uploadpic" value="Upload Image" />
        </form>
    </body>
</html>