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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度