2

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.

Iridium
  • 113
  • 1
  • 8
  • The server handles where "tmp" uploads go – GetSet Dec 21 '20 at 22:02
  • @GetSet I configured the uploads to go to the *uploads* folder in the *config.php* file, but they don't show up. – Iridium Dec 22 '20 at 09:36
  • @Iridium am facing a similar issue, If you we're able to solve the issue please post the solution as answer to this question – Rilla Apr 27 '21 at 08:59
  • 1
    @Rilla Unfortunately I wasn't able to solve the issue and I am using [jQuery-fileupload](https://github.com/blueimp/jQuery-File-Upload) instead now, which works well. It's also quite easy to design your own fileupload front-end just using html/css with this library. – Iridium Apr 28 '21 at 07:05
  • Thanks a lot for the feedback, from some research. I have been able to make it work. I wrote my own server for filepond, instead of using the filepond default php server on github. – Rilla Apr 28 '21 at 15:47
  • @Rilla Maybe you can post your github repo (if you would like to share your implementation) as an answer and I will mark it as solved. – Iridium Apr 29 '21 at 06:54
  • Okay I would do that ASAP – Rilla Apr 30 '21 at 07:13
  • 1
    @Iridium sorry for the late response. I just shared the link to the repo – Rilla May 15 '21 at 17:40
  • How to get the uploaded filenames in form submit so can save those filenames in the database ? – Noob Coder Aug 27 '23 at 07:50

1 Answers1

3

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/

Rilla
  • 505
  • 4
  • 16