How do I fix my PHP Form Redirecting to upload files to server

martinb

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.

Taylor Stocks

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix my faulty HTML form with JavaScript?

How can I fix my mistake about uploading files with PHP?

Where do I add my PHP form handler files in WordPress?

How do I monitor who is loading files on my server ? (Apache PHP)

Webpack 2 workflow how do I test my production files and how do I upload?

How do I write a file to my server with the contents from a form?

How do I fix my edit form from editing all the articles in my database in Ruby/Rails?

How do I upload files to the server using the drf-chunked-upload library?

How do I create my own server I can upload things to (like google drive) with Ubuntu server

How to upload changed files to my server via ftp, when i check in visual studio?

Why is my form posting after ajax post? How do I fix this?

How do I fix floating CSS label always being active showing 'invalid input' in my form?

How do I fix my neomuttrc syntax?

How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

How do I fix my FirebaseRecyclerAdapter on a fragment?

How do I fix plotting my boxplot?

How do I make my c# Discord Bot notify my server when I upload a YouTube video to my channel

Where do I put my website php files in my virtual ubuntu server?

How do I set up my server to work with PHP

What java android files should I not upload to my SVN Server?

How do I programmatically upload files to JupyterLab from my local storage?

Upload files to my Apache server

How do I fix my command that is supposed to spam a message in a discord server

How do I fix my code for insert into SQL Server with Python 3.7

What should I do to fix this error when sending the form to the server?

How do i upload files into one colum to server using entity framework?

How to upload files from form to server and store in a new folder

How to upload files to server from html form using python cgi

How can i upload a video and a picture at the same time to a server using php and my sql