dongzaotiao2863 2019-04-28 16:25
浏览 48
已采纳

如何修复PHP表单重定向以将文件上载到服务器

please help. I am trying to create a drag and drop file uploader with php and javascript but the when I click on upload files the php file shows the following error: This page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500

Here is the body of my html.

<body>
        <div class="wrapper">
            <div class="upload-console">
                <h2 class="upload-console-header">Upload</h2>

                <div class="upload-console-body">
                    <h3>Select files from your computer</h3>
                    <form action="upload.php" method="post" enctype="multipart/form-data">
                        <input type="file" name="files[]" id="standard-upload-files" multiple>
                        <input type="submit" value="Upload files" id="standard-upload">
                    </form>

                    <h3>Or drag and drop files below</h3>
                    <div class="upload-console-drop" id="drop-zone">
                        Just drag and drop files here
                    </div>

                    <div class="bar">
                        <div class="bar-fill" id="bar-fill">
                            <div class="bar-fill-text" id="bar-fill-text"></div>
                        </div>
                    </div>

                    <div id="uploads-finished" class="hidden">
                        <h3>Processed files</h3>
                    </div>      
                </div>
            </div>
        </div>
        <script src="js/upload.js"></script>
        <script src="js/global.js"></script>
    </body>

Here is my upload.php file.

<?php
    header('Content-Type: application/json');

    $allowed = ['mp4','png','jpg'];
    $processed = [];

    foreach($_FILES['files']['name'] as $key => $name) {
        if($_FILES['files']['error'][$key] === 0) {

            $temp = $_FILES['files']['tmp_name'][$key];

            $ext = explode('.',$name);
            $ext = strtolower(end($ext));

            $file = uniqid('',true) . time() . '.' . $ext;

            if(in_array($ext, $allowed) && move_uploaded_file($temp, 'uploads/' . $file)) {
                $processed[] = array(
                    'name' => $name,
                    'file' => $file,
                    'uploaded' => true
                );
            } else {
                $processed[] = array(
                    'name' => $name,
                    'uploaded' => false
                );
            }
        }
    } 
    echo json_encoded($processed);
?>

Here are results of running the tree command

.
├── css
│   └── global.css
├── index.html
├── js
│   ├── global.js
│   └── upload.js
├── phpinfo.php
├── upload.php
└── uploads
    ├── 5cc57ea6322ba7.448603261556446886.jpg
    ├── 5cc58390d82095.937935921556448144.jpg
    ├── 5cc583f48b4a81.475000611556448244.jpg
    ├── 5cc596bb0b2980.525819171556453051.jpg
    ├── 5cc5ca069f34e8.919505521556466182.jpg
    └── 5cc5ccc7525ad4.537311361556466887.jpg

Images appear to be uploading but I get a 500 error page. I'm trying to get it to get the progress bar to show progress and change the class hidden to show the downloaded files. But it redirects to uploads.php with a 500 error. Thank you.

All of the html and js can be seen here on codepen.

  • 写回答

2条回答 默认 最新

  • doujia7162 2019-04-28 16:29
    关注

    Your form will redirect to the 'action' inside it, in your case it's upload.php - if you want to stop it redirecting, you will need to add a php redirect: header("Location: mypage.php"); in order to get back to your previous page.

    If your pages are dynamic and you don't know what mypage.php is, add a hidden input in your form and use the page name as the value for this.

    Edit: My apologies, this still doesn't solve your issue - the redirect above will be for when your script has been executed. The 500 error is because of an error in your code. This error (as mentioned by @Chris White in your comments) is echo json_encoded($processed); This should be echo json_encode($processed);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题