Esempio n. 1
0
/**
 * Save the uploaded file
 *
 * @param string $currentPath The path to the uploaded file
 * @param string $filename Desired filename
 * @param iPhorm_Element_File $element The iPhorm file element
 * @param int $formId  The ID of the form
 */
function iphorm_save_uploaded_file($currentPath, $filename, iPhorm_Element_File $element, $formId)
{
    if (($wpUploadsDir = iphorm_get_wp_uploads_dir()) !== false) {
        // Get the save path
        $path = $element->getSavePath() == '' ? 'iphorm/{form_id}/{year}/{month}/' : $element->getSavePath();
        // Replace placeholders
        $path = str_replace(array('{form_id}', '{year}', '{month}', '{day}'), array($formId, date('Y'), date('m'), date('d')), $path);
        // Apply any filter hooks to the path
        $path = apply_filters('iphorm_upload_path', $path, $element);
        $path = apply_filters("iphorm_upload_path_{$formId}", $path, $element);
        // Join the path with the WP uploads directory
        $absolutePath = rtrim($wpUploadsDir, '/') . '/' . ltrim($path, '/');
        // Apply filters to the absolute path
        $absolutePath = apply_filters('iphorm_upload_absolute_path', $absolutePath, $element);
        $absolutePath = apply_filters("iphorm_upload_absolute_path_{$formId}", $absolutePath, $element);
        // Add a trailing slash
        $absolutePath = trailingslashit($absolutePath);
        // Make the upload directory if it's not set
        if (!is_dir($absolutePath)) {
            wp_mkdir_p($absolutePath);
        }
        // Check if the file name already exists, if so generate a new one
        if (file_exists($absolutePath . $filename)) {
            $count = 1;
            $newFilenamePath = $absolutePath . $filename;
            while (file_exists($newFilenamePath)) {
                $newFilename = $count++ . '_' . $filename;
                $newFilenamePath = $absolutePath . $newFilename;
            }
            $filename = $newFilename;
        }
        // Move the file
        if (rename($currentPath, $absolutePath . $filename) !== false) {
            chmod($absolutePath . $filename, 0644);
            return array('path' => $path, 'fullPath' => $absolutePath . $filename, 'filename' => $filename);
        } else {
            return false;
        }
    } else {
        // Uploads dir is not writable
        return false;
    }
}
Esempio n. 2
0
 /**
  * Add an attachment element, the uploaded file will
  * be added as an attachment to the email
  *
  * @param iPhorm_Element_File $element
  */
 public function addAttachmentElement(iPhorm_Element_File $element)
 {
     $this->_attachmentElements[$element->getName()] = $element;
 }