doujie9252 2014-10-16 05:47
浏览 51

PHP图像上传:作为一个孤立的示例,但不是我的项目代码

I have the most simple case of image upload. When I run this as a separate example it DOES work. When I incorporate the same .js code into my project's javascript code, and try the upload from my html page (.js and .php codes identical, id's and names adjusted etc.), it doesn't work.

html:

<html>
<head>
    <title>HTML5 File API</title>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <div id="main">
        <h1>Upload Your Images</h1>
            <input type="file" name="images" id="images"/>
            <input type="submit" id="submitImages" onclick="submitImages()">

    <div id="response"></div>
        <ul id="image-list">

        </ul>
    </div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  <script src="upload.js"></script>
</body>
</html>

javascript:

function submitImages(){

    if (window.FormData) {
        formdata = new FormData();
    }

    file = document.getElementById('images').files[0];

    if (!!file.type.match(/image.*/)) {
        if (formdata) {
            formdata.append("images[]", file);
        }
    }   

    if (formdata) {
        $.ajax({
            url: "upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {
                console.log(res); 
            }
        });
    }   

}

php:

<?php

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "uploads/" . $_FILES['images']['name'][$key]);
    }
}

echo "<h2>Successfully Uploaded Images</h2>";

?>

The message I get from server when I attempt the upload from my project:

POST-->POST:
Source
-----------------------------2656620091882 Content-Disposition: form-data; name="images[]"; filename="dock_2.jpg" Content-Type: image/jpeg ÿØÿáa"Exif��MM�*�� 
etc.

POST-->RESPONSE:
<br />
<b>Notice</b>:  Undefined index: images in <b>C:\XAMPP\htdocs\Projects\DockMapper\add_edit_upload_images.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\XAMPP\htdocs\Projects\DockMapper\add_edit_upload_images.php</b> on line <b>3</b><br />
<h2>Successfully Uploaded Images</h2>

It seems that something is sent to the server, but that it is not received on the server. When I run: print_r($_FILES); the array is empty. I spent probably two days over this and I couldn't find the problem. Any suggestions? Big thanks.

  • 写回答

2条回答 默认 最新

  • duanshangying5102 2014-10-16 05:53
    关注

    Use this For example Jquery

    $('#upload').on('click', function() {
        var file_data = $('#sortpicture').prop('files')[0];   
        var form_data = new FormData();                  
        form_data.append('file', file_data)
        alert(form_data);                             
        $.ajax({
                    url: 'upload.php',
                    dataType: 'text',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         
                    type: 'post',
                    success: function(data){
                        alert(data); 
                    }
         });
    });
    

    PHP

    <?php
    
        if ( 0 < $_FILES['file']['error'] ) {
            echo 'Error: ' . $_FILES['file']['error'] . '<br>';
        }
        else {
            move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
        }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值