从MAC照片应用程序上传到PHP表单
Uploading from MAC Photos app to PHP form
我有一个用户在从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();)
,看看它是否只是一个验证问题。
注意:您只是在验证文件扩展名——这是一个巨大的安全风险。
相关文章:
- 传递给 PHP 表单时 JavaScript 动态变量上的未定义索引
- 带有条件字段的PHP表单
- PHP表单提交和重定向
- 如何防止丢失PHP表单中输入的输入字段
- 在动态加载的PHP表单上放置JavaScript事件
- 正在创建'谢谢你'使用colorbox弹出PHP表单
- 带有jquery验证问题的php表单
- 如何将我通过JavaScript计算的自定义金额作为金额发送到PHP表单
- 提交时将JavaScript变量转换为PHP表单
- 带有多个提交按钮的PHP表单
- php表单验证n在表单上显示消息3秒钟并淡出
- php表单发布时正在加载进度条
- 带有javascript按钮的PHP表单-不起作用
- PHP表单适用于索引,但不适用于联系我们页面
- Cake php表单助手.替换输入值
- 对php表单的修改
- 如何在刷新时显示PHP表单中的随机文本和图像
- 自动完成PHP表单选择字段中的用户姓氏
- php表单中的JQuery-重定向到成功页面
- PHP表单,MailFoction和Google reCaptcha验证问题