Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4387872 to your computer and use it in GitHub Desktop.
Save anonymous/4387872 to your computer and use it in GitHub Desktop.
Integration between Wordpress and File Uploader (https://github.com/valums/file-uploader/tree/2.1.2).
<?php
// inicializa Wordpress
require_once( dirname(__FILE__) . '/../../../wp-load.php' );
// list of valid extensions, ex. array("jpeg", "xml", "bmp")
$allowedExtensions = array("jpeg", "png", "jpg");
// max file size in bytes
$sizeLimit = 2 * 1024 * 1024;
require( get_theme_root() . '/' . get_template() . '/fileuploader/uploader.php' );
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
// destination path - uploads/formrestrita/[slug of the user]
$wp_upload_dir = wp_upload_dir();
$targetPath = "{$wp_upload_dir['basedir']}/formrestrita/{$_REQUEST['user']}";
// create the path, if don't exist
if( ! is_dir( $targetPath ) ) {
mkdir( $targetPath );
chmod( $targetPath, 0777 );
}
$targetPath .= "/{$_REQUEST['name']}";
// create the field path, if don't exist
if( ! is_dir( $targetPath ) ) {
mkdir( $targetPath );
chmod( $targetPath, 0777 );
}
$fileName = slugify( $fileParts['filename'] );
// Call handleUpload() with the name of the folder, relative to PHP's getcwd()
$result = $uploader->handleUpload( $targetPath );
if( isset( $result['success'] ) && $result['success'] ) {
// save the information in the database
$user = get_user_by('login', $_REQUEST['user']);
$files = get_user_meta( $user->ID, 'files_area_restrita', true );
$files[$_REQUEST['name']][] = $result['file'];
update_user_meta( $user->ID, 'files_area_restrita', $files );
$result['file_url'] = "{$wp_upload_dir['baseurl']}/formrestrita/{$_REQUEST['user']}/{$_REQUEST['name']}/{$result['filename']}";
$result['remove_url'] = get_bloginfo( 'siteurl' ) . "/area-restrita/remove-upload/{$user->user_login}/{$result['filename']}";
$result['image'] = get_bloginfo( 'template_url' ) . "/images/cancel.png";
}
// to pass data through iframe you will need to encode all html tags
echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment