我一直试图在http://tuts.wtfdiary.com/2012/06/file-upload-in-php-without-refreshing.html使用本教程,而且我没有任何运气将其推入我自己的网站。我甚至一字一句地写下了剧本,但仍然没有 - 有什么理由可以解决这个问题吗?我甚至将.live更改为.on并更改了JQuery版本。我想知道这与提交有什么关系吗?谢谢
以下是我正在使用的代码:
<div id="formArea" class="userProfileFormWidth">
<script type="text/javascript" >
$(document).ready(function() {
$('#photoimg').live('change', function()
{
$("#preview").html('');
$("#current").hide();
$("#preview").html('<img src="ajax-loader.gif" alt="Uploading file...."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
});
</script>
<form id="imageform" method="post" enctype="multipart/form-data" action='update-user-profile.php'>
Upload image from your computer: <input type="file" name="photoimg" id="photoimg" /><br><br/>
</form>
<div id='preview'></div>
</div>
更新用户profile.php
<?php
session_start();
include("php/db-connect.php");
$get_user_sql = "SELECT * FROM members WHERE username = '$user_username'";
$get_user_res = mysqli_query($con, $get_user_sql);
while($user = mysqli_fetch_array($get_user_res)){
$user_id = $user['id'];
}
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$upload_avatar_sql = "UPDATE profile_members SET avatar='$actual_image_name' WHERE id='$user_id'";
$upload_avatar_res = mysqli_query($con, $upload_avatar_sql)or die(mysqli_error());
echo "<img src='uploads/".$actual_image_name."' class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1024k";
}
else
echo "Invalid file format..";
}
else
echo "No file selected";
exit;
}
?>
答案 0 :(得分:0)
有任何错误消息吗?尝试查看/调试您的代码,在谷歌浏览器中只需按f12并在控制台中单击以查看出现了什么问题