我一直在尝试在http://tuts.wtfdiary.com/2012/06/file-upload-in-php-without-refreshing.html上使用本教程,并且我没有将它运用到我自己的站点中。我甚至一字不提地复制了脚本,并且仍然没有任何内容 - 有没有任何理由说明这可能无效?我甚至将.live更改为.on并更改了JQuery版本。我想知道是否与提交有关?谢谢有没有人用过这个? (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;
}
?>
请通过将其粘贴到您的问题中并向其询问具体问题,向我们显示您的代码。什么不行?控制台中是否有错误? – 2013-03-24 13:46:08