Javascript Ajax&;PHP上传文件

Javascript Ajax&;PHP上传文件,javascript,php,ajax,upload,Javascript,Php,Ajax,Upload,我开始使用javascript进行web编程,但在上传文件时遇到了一些问题 我发现: 但我不知道如何在mvc项目中使用upload.php文件。使用什么作为表单的动作,以及应该在何处粘贴此php代码 有人能给我一些提示吗?如果你用php创建了一个控制器,你可以用ajax上传你的文件: <?php $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // Autorized extensions $max_size = 200

我开始使用javascript进行web编程,但在上传文件时遇到了一些问题

我发现: 但我不知道如何在mvc项目中使用upload.php文件。使用什么作为表单的动作,以及应该在何处粘贴此php代码


有人能给我一些提示吗?

如果你用php创建了一个控制器,你可以用ajax上传你的文件:

<?php
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // Autorized extensions
    $max_size = 200 * 1024; // Max file size
    $path = 'uploads/'; // Folder where to upload
    if ($_SERVER['REQUEST_METHOD'] === 'POST') 
    {
        if( ! empty($_FILES['image']) ) 
        {
            // Get the extension of the file
            $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
            // Test the file format if it's allowed
            if (in_array($ext, $valid_exts) AND $_FILES['image']['size'] < $max_size) 
            {
                $path = $path . uniqid(). '.' .$ext;
                // Put the file in the folder of uploads
                if (move_uploaded_file($_FILES['image']['tmp_name'], $path))
                    echo $path; // returning the path
                else
                    echo 'uploads/err.gif'; // returning the error message or path or whatever
            } 
            else 
                echo 'uploads/err.gif';
        } 
        else
            echo 'uploads/err.gif';
    } 
    else
        echo 'uploads/err.gif';
?>
ajaxUpload
函数的定义如下:

jQuery.fn.ajaxUpload = function(Button,Preview)
{
    var Frm = $(this); // form
    var Btn = Button; // upload button
    var Prev = Preview; // preview area 
    Btn.click(function()
    {
        // implement with ajaxForm Plugin
        Frm.ajaxForm(
        {
            beforeSend: function()
            {
                Btn.attr('disabled', 'disabled');
                Prev.fadeOut();
            },
            success: function(Result)
            {
                Frm.resetForm();
                Btn.removeAttr('disabled');
                Prev.attr("src",Result).fadeIn();
            },
            error: function(Result)
            {
                Btn.removeAttr('disabled');
                Prev.attr("src",Result).fadeIn();
            }
        });
    });
};

您不使用PHP逐字记录。您可以重写它以适应MVC框架的任何约定。您必须将表单操作路径更改为上载文件路径路径。
jQuery.fn.ajaxUpload = function(Button,Preview)
{
    var Frm = $(this); // form
    var Btn = Button; // upload button
    var Prev = Preview; // preview area 
    Btn.click(function()
    {
        // implement with ajaxForm Plugin
        Frm.ajaxForm(
        {
            beforeSend: function()
            {
                Btn.attr('disabled', 'disabled');
                Prev.fadeOut();
            },
            success: function(Result)
            {
                Frm.resetForm();
                Btn.removeAttr('disabled');
                Prev.attr("src",Result).fadeIn();
            },
            error: function(Result)
            {
                Btn.removeAttr('disabled');
                Prev.attr("src",Result).fadeIn();
            }
        });
    });
};