We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8168
    • 1,118 Posts
    Hi,

    I have some great code working from the formit2file addon as per code below. But am looking for some PHP assistance to allow a file input field to be able to upload more than just 1 file using this code... currently my form has a number of file inputs, and one of them allows for multiple file selection / uploads - this code only uploads 1 of the x files selected on this multiple upload input for some reason...

    Can anyone advise how to adapt it to make it work as I need it?

    Oh... and would also need the paths of the files uploaded to be saved back as the value of the inputs if possible... Possible?

    Thanks!


    // initialize output;
    $output = true;
     
    // valid extensions
    $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf', 'jpg', 'jpeg', 'png', 'eps', 'svg', 'gif', 'ai');
     
    // create unique path for this form submission
    //$uploadpath = 'assets/uploads/';
     
    // you can create some logic to automatically
    // generate some type of folder structure here.
    // the path that you specify will automatically
    // be created by the script if it doesn't already
    // exist.
     
    // UPLOAD TO FOLDER IN /ASSETS/UPLOADS/ WITH ID OF THE PARENT PROJECT FOLDER RESOURCE 
    
    // Get page ID
    // $pageid = $modx->resource->get('id');
    // $uploadpath = 'assets/uploads/'.$pageid.'/';
    
    // Get parent page title
    $parentObj = $modx->resource->getOne('Parent');
    $parentpageid = $parentObj->get('pagetitle');
    $uploadpath = 'assets/uploads/'.$parentpageid.'/';
     
    // get full path to unique folder
    $target_path = $modx->config['base_path'] . $uploadpath;
     
    // get uploaded file names:
    $submittedfiles = array_keys($_FILES);
     
    // loop through files
    foreach ($submittedfiles as $sf) {
     
        // Get Filename and make sure its good.
        $filename = basename( $_FILES[$sf]['name'] );
     
        // Get file's extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $ext = mb_strtolower($ext); // case insensitive
     
        // is the file name empty (no file uploaded)
        if($filename != '') {
             
            // is this the right type of file?
            if(in_array($ext, $ext_array)) {
         
                // clean up file name and make unique
                $filename = mb_strtolower($filename); // to lowercase
                $filename = str_replace(' ', '_', $filename); // spaces to underscores
                $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
                 
                // full path to new file
                $myTarget = $target_path . $filename;
                 
                // create directory to move file into if it doesn't exist
                mkdir($target_path, 0755, true);
                 
                // is the file moved to the proper folder successfully?
                if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                    // set a new placeholder with the new full path (if you need it in subsequent hooks)
                    $modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
                    // set the permissions on the file
                    if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                     
                } else {
                    // File not uploaded
                    $errorMsg = 'There was a problem uploading the file.';
                    $hook->addError($sf, $errorMsg);
                    $output = false; // generate submission error
                }
             
            } else {
                // File type not allowed
                $errorMsg = 'Type of file not allowed.';
                $hook->addError($sf, $errorMsg);
                $output = false; // generate submission error
            }
         
        // if no file, don't error, but return blank
        } else {
            $hook->setValue($sf, '');
        }
     
    }
     
    return $output;
    
    [ed. note: dubbs last edited this post 7 years, 7 months ago.]
    • You could use the AjaxUpload Extra …
        • 8168
        • 1,118 Posts
        Quote from: Jako at Aug 29, 2016, 08:56 PM
        You could use the AjaxUpload Extra …

        Thanks - does that support multi uploads then?

        Think it might be easier to adapt formit2file rather than start again with ajax uploader?
        • Yes, it supports uploading multiple files in AJAX mode before the form submit.
            • 52700
            • 15 Posts
            ajax upload not displasying