单个文件上传jQuery PHP

假装

我敢肯定我在这个错误上做了一个简单的错误。该文件未传递到php页面(我从PHP页面传回“未上传:文件错误。请重试。”错误。)

没有JQUERY的代码就可以正常工作,所以我想PHP不是问题。

我已经检查了字段名称/ ID,看来还可以。我尝试进行测试以查看按钮的值是否通过了,但没有通过,那是我得出的结论可能是JQUERY,但后来我很沮丧。

我知道已经提交了类似的帖子(我已经阅读过),但是我很绝望!

先感谢您。

这是HTML:

    <form action="upload.php" name="formname" ENCTYPE="multipart/form-data" id="formid">
        <fieldset>
            <legend>File info</legend>
            <p>
                <label for="file1">Files<span class="red"> *</span></label>
                <input name="file1" type="file" />
            </p>
        </fieldset>
        <p><label>&nbsp;</label><input type="button" id="submit" value="Add" /></p>
    </form>
    <div id="output"></div>

<script>
$(function(){
    $('#submit').on('click', function(){ 
        var fd = new FormData($("#formid"));
        $.ajax({
            url: 'upload.php',  
            type: 'POST',
            data: fd,
            success:function(data){
                $('#output').html(data);
            },
            cache: false,
            contentType: false,
            processData: false
        });
    });
});
</script>

和PHP

<?php
    $strName = 'JoeBloggs';
    $intId = 1045;
    $missing = '';
    date_default_timezone_set("Europe/London");
    error_reporting(E_ALL);
    include('class.upload.php');
    // where to put the images?
    $dir_dest = '../'.$intId.'/';
    $xcount = 0;
    if (!file_exists($dir_dest)) { mkdir($dir_dest, 0777, true);}
    # upload 1400px
    $handle = new upload($_FILES['file1']);
    if ($handle->uploaded) {
        $y = $handle->image_src_y;
        $height = $y/2;
        $handle->image_resize           = true;
        $handle->image_ratio_crop       = true;
        $handle->image_x                = 700;
        $handle->image_y                = 260;
        $handle->image_convert          = jpg;
        $handle->Process($dir_dest);
        if ($handle->processed) {
            // everything was fine !
            $strFilename1 = $handle->file_dst_name;
            rename( $dir_dest . $strFilename1, $dir_dest . $strName . '.jpg' );
            $missing .= 'File uploaded';
        } else {
            $missing .= $missing. 'Not processed:' . $handle->error . '<br />';
        }
    } else {
        $missing .= 'Not uploaded:' . $handle->error . '<br />';
    }

    echo $missing;
    //header('Location: ../index.php?missing='.$missing);
?>
<img src="<?= $dir_dest . $strName?>.jpg" />
1994年的Keyur Chavda-kc

试试这个可能对您有帮助,

<script>
 $(function(){
  $('#submit').on('click', function(){ 
    var form = $('#formid')[0];
    var formData = new FormData(form);
    $.ajax({
        url: 'upload.php',  
        type: 'POST',
        data: formData,
        success:function(data){
            $('#output').html(data);
        },
        cache: false,
        contentType: false,
        processData: false
    });
});
});
</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章