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

Uploading from MAC Photos app to PHP form

本文关键字:PHP 表单 程序上 应用程序 MAC 照片 应用      更新时间:2023-09-26

我有一个用户在从MacBook Air Photos应用程序上传时遇到问题。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照片错误警报同样,当他们在应用程序之外正常上传时(就像PC用户一样),他们不会得到这个。

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

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