我一直在嘗試在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