<![CDATA[ Upload Images to a Gallery Album with Media Source and TV - My Forums]]> https://forums.modx.com/thread/?thread=104498 <![CDATA[Upload Images to a Gallery Album with Media Source and TV]]> https://forums.modx.com/thread/104498/upload-images-to-a-gallery-with-album-media-source-and-tv#dis-post-562043
> Here is my form:
[[!+fi.validation_error_message:notempty=`<h1 class="error" style="color:Tomato;"><p>[[!+fi.validation_error_message]]</p></h1>`]]
<form method="post" action="">
	    <div class="row gtr-50 gtr-uniform">
	        <div class="col-6 col-12-mobilep">
                    <div class="row clearfix">
                    <div class="label">Supplementary Files <br /><span class="error">[[+fi.error.nomination_file]]</span></div>
                    <!-- end of .label -->
                    <div class="input"><input id="nomination_file" type="file" name="nomination_file" value="[[+tv.foto_perfil]]" maxlength="100000" /></div>

                    <div class="col-12">
                <ul class="actions special">
                    <li><input type="submit" value="Upload"/></li>
            </ul>
            </div>
        </div>
        </div>
        </div>
</form>


> here is the snippet call:
[[!formIt? 
    &redirectTo=`27`
    &hooks=`formit2file`
    &path = `assets/gallery/3/`
    &extensions = `jpg,JPG,jpeg,JPEG,doc,docx,pdf,txt,png,zip,mov,avi,mpeg,mp4`
    &maxsize = `100`
    &validate=`nomination_file:required`]]

And here is the form2file snippet code:
  <?php
$path; // Path from root that user specifies
$extensions; // allow file extensions
 
$ext_array = explode(',', $extensions);
 
// Create path
$basepath = $modx->config['base_path']; // Site root
$target_path = $basepath . $path; // root /assets/upload
 
//$target_path = '/home/emeraldfound/emeraldfoundation.ca/assets/uploads/';
 
// Get Filename and make sure its good.
$filename = basename( $_FILES['nomination_file']['name'] );
 
// Get files extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
 
if($filename != '')
{
    // Make filename a good unique filename.
    // Make lowercase
    $filename = mb_strtolower($filename);
    // Replace spaces with _
    $filename = str_replace(' ', '_', $filename);
    // Add timestamp
    $filename = date("Ymdgi") . $filename;
 
    // Set final path
    $target_path = $target_path . $filename;
 
    if(in_array($ext, $ext_array))
    {
        if(move_uploaded_file($_FILES['nomination_file']['tmp_name'], $target_path))
        {
            // Upload successful
            //$hook->setValue('nomination_file',$_FILES['nomination_file']['name']);
            $hook->setValue('nomination_file',$filename);
            return true;
        }
        else
        {
            // File not uploaded
            $errorMsg = 'File not uploaded.';
            $hook->addError('nomination_file',$errorMsg);
            return false;
        }
    }
    else
    {
        // File type not allowed
        $errorMsg = 'File not allowed.';
        $hook->addError('nomination_file',$errorMsg);
        return false;
    }
}
else
{
    $hook->setValue('nomination_file','');
    return true;
}
 
return true;



]]>
ilinko Oct 11, 2018, 10:43 PM https://forums.modx.com/thread/104498/upload-images-to-a-gallery-with-album-media-source-and-tv#dis-post-562043