douyu7210 2013-03-24 13:43
浏览 26
已采纳

有没有人用过这个? (JQuery上传)[关闭]

I've been trying to use this tutorial at http://tuts.wtfdiary.com/2012/06/file-upload-in-php-without-refreshing.html and I haven't had any luck with implimenting it into my own site. I've even coppied the script word for word and still nothing - Are there any reasons why this may not work? I've even changed the .live to .on and changed the JQuery version. I'm wondering if it's anything to do with submit? Thank you

Here is the code I'm using:

<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>

update-user-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;
}

?>
  • 写回答

1条回答 默认 最新

  • duannuo4620 2013-03-24 14:02
    关注

    there are any error messages? try to look/debug your code, in google chrome just push f12 and click in Console to see what's going wrong

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)