【发布时间】:2016-10-11 17:56:44
【问题描述】:
有人可以找出我做错了什么吗?我从我网站上的另一个页面复制了它,但我无法让它在这个页面上工作。我还遗漏了一些文本输入,它们按预期工作。谢谢。
<?php
if (isset($_POST["save"])) {
if (isset($_POST['pic'])){
//isset never detected when image is uploaded in html
}
}
<html>
<form class="form-horizontal" onsubmit="return checkSize(2097152)" enctype="multipart/form-data" data-toggle="validator" method="POST" action="profile.php">
<input id="pic" name="pic" type="file" accept="image/*" class="text-center center-block well well-sm">
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary" name="save" id="save">Save Changes</button>
<a href="profile.php" class="btn btn-default" role="button">Cancel</a>
</div>
</div>
</form>
<script type="text/javascript">
//check image size func
function checkSize(max_img_size)
{
var input = document.getElementById("pic");
// check for browser support (may need to be modified)
if(input.files && input.files.length == 1)
{
if (input.files[0].size > max_img_size)
{
alert("Profile Picture must be less than " + (max_img_size/1024/1024) + "MB");
return false;
}
}
return true;
}
补充:我刚刚做了一个 print_r($_POST); print_r($_FILES);并且没有上传图片:
Array ( [nickname] => raySirSharp [motto] => Big gulps eh? [email] => razer88@hotmail.com [submit] => ) Array ( [pic] => Array ( [name] => [类型] => [tmp_name] => [错误] => 4 [大小] => 0 ) )
上传图片:
Array ( [nickname] => raySirSharp [motto] => Big gulps eh? [email] => razer88@hotmail.com [submit] => ) Array ( [pic] => Array ( [name] => 12472627_211490652554223_5803523050459466916_n.jpg [type] => image/jpeg [tmp_name] => /tmp/phpvNCBzj [error] => 0 [size] => 70254 ) )
我可以使用什么来检测图片是否已上传?
我试过了:
if (isset($_FILES['pic'])){ //always true :( }
【问题讨论】:
-
onsubmit 中的 checkSize() 函数是什么?
-
嘿@Ryan,请在您的答案中添加java脚本功能代码
-
你的 PHP 标签关闭了吗?文件保存为 PHP 文件? js源码?