如何在用户上传具有正确规格的图像之前禁用提交按钮?

时间:2011-06-27 07:58:12

标签: php upload

我有这个上传代码:

<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "png") && ($_FILES["uploaded_file"]["type"] == "image/png") && 
    ($_FILES["uploaded_file"]["size"] < 150000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .ppg images under 150Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

而且我还在使用jquery表单插件来使用ajax提交表单。

问题是,除非用户尝试上传最大为150 KB的PNG,否则如何禁用提交按钮?

2 个答案:

答案 0 :(得分:1)

您需要单独上传文件,因为您无法检查上传输入字段中文件名所指向的文件大小。

但您可以检查文件扩展名。在符合标准的浏览器上,您可以执行以下操作以显示文件的名称:

<input type="file" id="test">
<script type="text/javascript">
  var input = document.getElementById('test');
  input.addEventListener('change', function() { alert(input.value); }, false);
</script>

答案 1 :(得分:0)

如果您使用html表单让用户上传图片,则无法完成此操作(html尝试保护用户免受脚本上传不同于用户指定的文件的限制,并限制网站开发人员使用修改上传按钮)。

但是有一个名为uploadify的jquery插件允许更多自定义,这可能有助于您解决方案。