Warm tip: This article is reproduced from serverfault.com, please click

Creating PHP backend for file upload using filepond

发布于 2020-12-21 21:53:00

I am creating a form on a website, where files (images and pdfs) need to be uploaded too. Until now, I have used a simple input type="file" element, coupled to a PHP file on the backend (snippet follows):

$allowed = array('jpg', 'jpeg', 'pdf', 'png');
if(isset($_FILES['uploadctl']) && $_FILES['uploadctl']['error'] == 0){
    $extension = pathinfo($_FILES['uploadctl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"not_allowed"}';
        exit;
    }

    // create folder to upload files to
    $id = session_id();
    $user_folder = 'user_data/' . $id;
    if( is_dir($user_folder) === false ){
        mkdir($user_folder); 
    }
    if(move_uploaded_file($_FILES['uploadctl']['tmp_name'], $user_folder . "/" . $_FILES['uploadctl']['name'])){
        echo '{"status":"success"}';
        exit;
    }
    echo '{"status":"error"}';
}

This works well. However, I would like more functionality for the upload form and have looked into filepond. I created the filepond object as per the documentation and copied the boilerplate code to ./file-pond-assets, which I plan to adapt to my needs later:

<input type="file" name="uploadctl" multiple accept=".pdf,.png,.jpg,.jpeg">
<script>
            const inputElement = document.querySelector('input[type="file"]');
            const pond = FilePond.create( inputElement );
            pond.setOptions({
                server: './file-pond-assets'
            });
</script>

which is showing when displaying the website. When trying to upload a file, the front-end looks fine, as an upload complete message appears. However, I cannot find the uploaded files in the tmp and uploads folder inside ./file-pond-assets. I tried changing permissions of the folders and also checked the console, but cannot find an error message. The config.php file also points to the right folders. What do I miss that makes my files not appear on my server? I would like to keep the upload as a multipart/form-data.

Questioner
Iridium
Viewed
0
Rilla 2021-05-16 01:39:31

Here is a link to my sample file-pond PHP server implementation repository on gihtub Repo Link: https://github.com/Onihani/filepond-php-server-example Live Preview: http://www.ics-courses.co.uk/natbongo/filepond-php-server-example/