php - 如何在PHP中使用Ajax上传图片?

标签 php jquery

我有一个表格

<form id="profile_imageform" class="image_form" enctype="multipart/form-data">

    <fieldset>
        <div class="entry">
            <label>Filename:</label>
            <input type="file" name="file" id="file" />
        </div>
    </fieldset>
    <div class="actions">
        <button type="submit">Save &amp; Close</button>( <a href="#/profile" class="cancel">Cancel</a> )
    </div>
</form>

我的 js 文件看起来像

ProfileForm.prototype.init = function(){ var self = this;

    //Initialize properties
    this.view = $( "#profile_form_view" );
    this.imageForm = $( "#profile_imageform" );
    this.fields.id = this.imageForm.find( ":input[ name = 'id' ]" );
    this.fields.image = this.imageForm.find( ":input[ name = 'file' ]" );

    //Bind the submit handler
    this.imageForm.submit(function( event ){

        //Submit the form
        self.saveImage(this.fields.id.val(), this.fields.image.val());

        //Cancel default event
        return( false );

    });

 ProfileForm.prototype.saveImage = function( id, image, onSuccess, onError ){
    var self = this;

    application.ajax({
        url: "test.php",
        data: {
            method: "saveImage",
            id: id,
            image: image
        },
        success: function( response ){
            if (response.success){
                onSuccess( response.data );
            }else{
                onError( response.errors );
            }
        }

    });
};

但是

this.fields.image.val() 

返回图像名称,我需要的是它的tmp_name。 可以在 jQuery 中获取它的 tmp_name 吗?如果是这样怎么办?

但是这个 php 代码也返回错误

if (isset($_POST['method']))
{
    if (isset($_POST['image']))
    {
        echo $_FILES['image']['tmp_name'];
    }
}

最佳答案

如果您不进行任何错误检查,则不要期望出现错误响应......

function file_upload_error_message($error_code) {
    switch ($error_code) {
    case UPLOAD_ERR_INI_SIZE:
        return 'The uploaded file exceeds the upload_max_filesize directive in    php.ini';
    case UPLOAD_ERR_FORM_SIZE:
        return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
    case UPLOAD_ERR_PARTIAL:
        return 'The uploaded file was only partially uploaded';
    case UPLOAD_ERR_NO_FILE:
        return 'No file was uploaded';
    case UPLOAD_ERR_NO_TMP_DIR:
        return 'Missing a temporary folder';
    case UPLOAD_ERR_CANT_WRITE:
        return 'Failed to write file to disk';
    case UPLOAD_ERR_EXTENSION:
        return 'File upload stopped by extension';
    default:
        return 'Unknown upload error';
}
} 

关于php - 如何在PHP中使用Ajax上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8641141/

相关文章:

php - 传递大变量时如何减少内存使用

php - 如何在PHP中找到25个连续列的平均值

PHP - 调用需要来自不同文件夹的文件并使用命名空间的未定义函数

javascript - 使用 jQuery 缩小文本搜索的选择范围

javascript - jQuery 循环中 div 的不同 CSS

javascript - 将 javascript Canvas 路径保存到数据库中的正确方法

php - 从 mysql 数据库中选择最长的天数?

javascript - 如何处理缓慢的 Ajax 请求/连接缓慢的人

HTML 中的 JavaScript 值与 JQuery

javascript - 如何在动态创建的文本框上应用按键和鼠标按下事件