我有一个ajax图像上传工作得很好的scripta.php,我也有scriptb.php使用下面相同的脚本ajax图像上传效果很好但是在scriptc.php我似乎无法理解为什么它不工作我不知道如果是因为模态框,它们都使用相同的脚本,两者运行良好,我想用传入的图像更新数据库并预览它供用户查看
<div id="myModal" class="modal custom fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" >×</button>
<h4 class="modal-title"><p style="color:black;font-weight:bold;">Edit and More info</p></h4>
</div>
<div class="modal-body">
//scriptc.php not working in modal box
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.form.js"></script>
<form id="imageform" method="post" style=" text-align:center;position:absolute;margin-top:105px;margin-left:550px;" enctype="multipart/form-data" action='updatechannelimg.php'>
<div class="fileUpload btn btn-primary">
<span><i class = "fa fa-camera"></i> Add Channel profile image</span>
<input type="file" name="photoimg" id="photoimg" class="upload" />
</div>
</form>
//this is an image preview script
<script>
$('#photoimg').live('change', function() {
$("#preview").html('');
$("#preview").html('<img style="margin-left:50px;height:20px;width:20px;" src="ajax.gif" alt="Uploading...."/> <br/> <b style="margin-left:25px;">Uploading</b>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
$('#photoimg').live('change', function() {
// $("#preview").html('');
// $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
$("#imageform").ajaxForm({
target: '#p'
}).submit();
});
</script>
//the preview itself
<a id="preview" style="margin-top:-50px;position:absolute;"></a>
<div class="modal-footer">
<button type="button" class="btn btn-default mada" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
//the php upload script updatechannelimg.php in the form action above
<?php
include ('db.php');
include ('connect.php');
$uni = $_COOKIE['uniz'];
$path = "channelimg/";
$valid_formats = array("jpg", "png", "PNG", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$nam = "unilag-".$name."-".rand(1000,100000)."-".$_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
$tmpi = $_FILES['photoimg']['tmp_name'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(12*12))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$s = mysqli_query($con, "SELECT * FROM `channels` WHERE channel_id = '$uni'");
$row = mysqli_fetch_array($s);
$img = $row['channel_img'];
if($img != '') {
unlink($path.$img);
}
mysqli_query($db,"UPDATE channels SET channel_img='$actual_image_name' WHERE channel_id = '$uni'");
echo "<img src='channelimg/".$actual_image_name."' style='object-fit:cover;' width='110px' height='110px' class='preview img-circle'>";
} else {
echo "Upload failed. Please try another jpeg image";
}
exit();
} else {
$ext = pathinfo($nam, PATHINFO_EXTENSION); //get image extension
if($ext=='png' || $ext=='PNG' ||
$ext=='jpg' || $ext=='jpeg' ||
$ext=='gif' || $ext=='GIF' ||
$ext=='JPG' || $ext=='JPEG'
)
//checking image extension
if($ext=='jpg' || $ext=='jpeg' || $ext=='JPG' || $ext=='JPEG')
{
$src=imagecreatefromjpeg($tmpi);
}
if($ext=='png' || $ext=='PNG')
{
@$src=imagecreatefrompng($tmpi);
}
if($ext=='gif' || $ext=='GIF')
{
$src==imagecreatefromgif($tmpi);
}
list($width_min,$height_min)=getimagesize($tmpi);//fetching original image height an width
$newwidth_min=200; //set compression image width
$newheight_min = ($height_min / $width_min) * $newwidth_min; //equation for compressed image height
$tmp_min= imagecreatetruecolor($newwidth_min, $newheight_min);//create frame for compress image
$white = imagecolorallocate($tmp_min, 255, 255, 255);
imagefill($tmp_min,0,0,$white);
imagealphablending($tmp_min, TRUE);
@imagecopyresampled($tmp_min, $src, 0,0,0,0,$newwidth_min, $newheight_min, $width_min, $height_min);//compress the image
if(@imagejpeg($tmp_min,$path.$nam,100)) //sending it to the destinationation folder
{
$s = mysqli_query($con, "SELECT * FROM `channels` WHERE channel_id = '$uni'");
$row = mysqli_fetch_array($s);
$img = $row['channel_img'];
if($img != '') {
unlink($path.$img);
}
mysqli_query($db,"UPDATE channels SET channel_img='$nam' WHERE channel_id = '$uni'");
echo "<img src='channelimg/".$nam."' style='object-fit:cover;' width='110px' height='110px' class='preview img-circle'>";
} else {
echo "Upload failed. Please choose a jpeg image";
}
}
}
}
}
?>