从MAC Photos应用程序上传到PHP表单

时间:2015-12-30 15:20:33

标签: javascript php macos upload photos

我的用户无法从MacBook Air照片应用上传。 MacBook只有2年的历史并且已经更新,他们可以在没有应用程序的情况下定期上传图片。我不能自己复制这个问题,并且很难解决这个问题。

Php代码在上传到服务器之前进行验证

    if(getimagesize($_FILES['image']['tmp_name']) == TRUE){ //files have tmp_names on server when being uploaded

  $name; $image; //Vars created here for use in retrieving image id from DB afterwords
  function saveImage($uid,$name,$image){
      //Update to get rid of default pic from registration
      $qry = "UPDATE profile_pics SET name='$name', image='$image' WHERE user_id='$uid';";
      $result=mysql_query($qry) or die(mysql_error());

      if($result){
          echo "<br/>Image uploaded.";
      }
      else{
          echo "<br/>Image not uploaded.";
      }
  }

  if($_FILES['image']['size'] < 1020000){ //1.02 since file size grows when uploaded to server.
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['image']['tmp_name']);
    $ok = false;
    switch ($mime) {
      case 'image/jpeg':
      case 'image/pjpeg':
      case 'image/png':
      case 'image/gif':
      case 'image/jpg':
        $ok = true;
        break;
      default:
      $errors[] = lang("ACCOUNT_PIC_WRONG_FILETYPE");
        // die("Unknown/not permitted file type");
    }
    if($ok == true){
      $image= addslashes($_FILES['image']['tmp_name']); //addslashes() function returns a string with backslashes in front of predefined
      $name= addslashes($_FILES['image']['name']);      //characters: ', ", \, NULL. To prepare string for storage in a database and database queries
      $image= file_get_contents($image);
      $image= base64_encode($image);
      saveImage($uid,$name,$image);
    }
  }else{
    $errors[] = lang("ACCOUNT_PICSIZE_TOO_LARGE");
  }
}

HTML / Javascript表单的一部分。 Javascript在进行后端之前会进行一些验证。这是为了在提交表单之前提醒用户错误。

 <script type="text/javascript">
  //Check to make sure pic size not to large
  jQuery(document).on("change", ".input-pic[type=file]", function () {
      var file = this.files[0];
      var size = file.size / 1024;
      var ext = file.name.split('.').pop();
      if (size > 1020) {
          alert("Too large Image. Only image smaller than 1MB (1000kB) can be uploaded.");
          //If too large, make field empty again
          jQuery(this).replaceWith('<input type="file" class="form-control input-pic" name="image" id="image">');
      }else if(ext == "jpeg" || ext == "pjpeg" || ext == "png" || ext == "gif" || ext == "jpg"){
        //Do nothing, valid image type
      }else{
        alert("Only jpeg, png, and gif pictures may be uploaded. You uploaded: ."+ext);
        jQuery(this).replaceWith('<input type="file" class="form-control input-pic" name="image" id="image">');
      }
  });
  </script>

  <div class='row form-row' style="padding-bottom:10px;">
    <div class='col-md-12'>
      <strong for="image">Upload a profile picture (JPG/PNG/GIF size limited to 1MB)</strong>
    <input type="file" class="input-pic form-control" name="image" id="image">
    </div>
  </div>

他们继续在javascript中收到提醒: MAC Photos error alert 同样,当他们正常地将其上传到应用程序之外时,他们不会得到这个(就像PC用户一样)。

1 个答案:

答案 0 :(得分:1)

我相信OS X正在将扩展名设置为大写。试试var ext = strtolower(file.name.split('.').pop();),看看它只是一个验证问题。

注意:您只是验证文件扩展名 - 这是一个巨大的安全风险。