dongwu5801 2012-07-23 10:36
浏览 20
已采纳

Ajax PHP上传脚本

Where do i put my PHP SQL query, to insert image information to my database? I tried just before the echo "success"; however it had no effect.

<!-- Upload Button-->
<div id="upload" >Upload File</div><span id="status" ></span>
<!--List Files-->
<ul id="files" ></ul>

PHP handling

<?php
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
echo "success";
} else {
echo "error";
}
?>

Javascript section

$(function () {
    var btnUpload = $('#upload');
    var status = $('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        //Name of the file input box
        name: 'uploadfile',
        onSubmit: function (file, ext) {
            if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                // check for valid file extension
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }
            status.text('Uploading...');
        },
        onComplete: function (file, response) {
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            if (response === "success") {
                $('<li></li>').appendTo('#files').html('<img src="./uploads/' + file + '" alt="" /><br />' + file).addClass('success');
            } else {
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });
});
  • 写回答

1条回答 默认 最新

  • douyuan9512 2012-07-23 10:47
    关注

    The query should be right before success echo, so you have to verify what is returning to you the move_uploaded_file method.

    Verify if ./upload/ is the right path and if you have read/write access (0777) on this directory.

    Also make sure that you're connected to database:

    <?php 
    
        $conn = mysql_connect("HOST","USER","PASSWORD");
        $db = mysql_select_db("DB_NAME");
    
        $uploaddir = './uploads/'; 
        $file = $uploaddir . basename($_FILES['uploadfile']['name']);  
    
        if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
            mysql_query("INSERT INTO photos VALUES ('your_values')");
            echo "success"; 
        } else { 
            echo "error"; 
        } 
     ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥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报错