get_results("SHOW TABLES LIKE '%$table_name%'") ) return FALSE; else return TRUE; } function af_mysql_warning() { echo '

WARNING! The AttachFiles MySQL databases were not created!

'; } /*------------------------------------------------------------- Name: af_show_existing Purpose: List any attachments that are found in the database Receive: -None- Return: -None- -------------------------------------------------------------*/ function af_show_existing() { if ( isset($_GET['post']) ) { // Initialize variables global $wpdb, $table_prefix, $af_site_url; // Existing files will only be available when "Editing", $_GET will hold Post ID // Look up existing files from database $sql = "SELECT * FROM ".$table_prefix."attached_files_meta WHERE post_id = $_GET[post] ORDER BY file_name ASC"; $results = $wpdb->get_results($sql); if ( count($results) > 0 ) { // Output file listing print '
Attached Files '."\n"; // Loop through attached files $i = 1; foreach ( $results as $file ) { if ( af_isEven($i) ) $class = ""; else $class = "alternate"; echo ' '."\n"; $i++; } print '
Filename Filesize (Bytes)
'.$file->file_name.' '.$file->file_size.' View/Download Delete
'."\n"; } } } /*------------------------------------------------------------- Name: af_insert_input Purpose: Prepare input form on advanced edit post page Receive: -None- Return: TRUE -------------------------------------------------------------*/ function af_insert_input() { global $af_config, $wpdb, $table_prefix; $upload_max = strlen($af_config['maxfilesize']) > 0 ? $af_config['maxfilesize'].'K' : ini_get('upload_max_filesize'); // Get Number of Uploads available if ( isset($_GET['post']) ) { $num_uploads = ( $af_config['maxfiles'] == '' ) ? 5 : ($af_config['maxfiles'] - $wpdb->get_var("SELECT COUNT(*) FROM ".$table_prefix."attached_files_meta WHERE post_id = $_GET[post]")); } else $num_uploads = ( $af_config['maxfiles'] == '' ) ? 5 : $af_config['maxfiles']; ?>

Attach Files

Add File(s) - ( bytes maximum) 0 ) { print '

You can upload files with the extension '.$af_config['extensions'].'

'."\n"; } for ( $i = 0; $i < $num_uploads; $i++ ) { ?>

 

get_var("SELECT COUNT(*) FROM ".$table_prefix."attached_files_meta WHERE post_id = $_GET[post]")); } else $num_uploads = ( $af_config['maxfiles'] == '' ) ? 5 : $af_config['maxfiles']; print ''."\n"; return TRUE; } /*------------------------------------------------------------- Name: af_process_upload Purpose: Process uploaded files. Move files to permanent location on filesystem and update mysql database. Receive: upload directory, array of uploaded filenames Return: boolean -------------------------------------------------------------*/ function af_process_upload($post_id) { global $table_prefix, $wpdb, $af_config; $STARTFILE = 0; $ONFILE = 'af_file'.$STARTFILE; // Loop through uploaded files while ( isset($_FILES[$ONFILE]) ) { $SrcPathFile = $_FILES[$ONFILE]["tmp_name"]; $SrcFileType = $_FILES[$ONFILE]["type"]; $DstFileName = $_FILES[$ONFILE]["name"]; if ( af_valid_extension(array_pop(explode('.', $DstFileName))) ) { /* -------------------------------------------------------- Check file against size restrictions ---------------------------------------------------------*/ if ( $af_config['maxfilesize'] > 0 ) { // File size in bytes $filesize[$ONFILE] = filesize($SrcPathFile); if ( $filesize[$ONFILE] > ( $af_config['maxfilesize'] * 1000 ) ) { header('Location: '.get_settings('siteurl') . "/wp-admin/options-general.php?page=attach-files.php&error=fsize"); die(); } } if ( $af_config['maxpp'] > 0 ) { if ( !isset($filesize[$ONFILE]) ) $filesize[$ONFILE] = filesize($SrcPathFile); // Determine combined size of files for this post $SQL = "SELECT SUM(file_size) FROM ".$table_prefix."attached_files_meta WHERE post_id = '".$post_id."';"; $post_size = $wpdb->get_var($SQL); if ( ( ( $filesize[$ONFILE] + $post_size ) / 1000 ) > $af_config['maxpp'] ) { header('Location: '.get_settings('siteurl') . "/wp-admin/options-general.php?page=attach-files.php&error=psize"); die(); } } if ( $af_config['sysquota'] > 0 ) { if ( !isset($filesize[$ONFILE]) ) $filesize[$ONFILE] = filesize($SrcPathFile); // Determine combined size of files system-wide $SQL = "SELECT SUM(file_size) FROM ".$table_prefix."attached_files_meta"; $system_size = $wpdb->get_var($SQL); if ( ( ( $filesize[$ONFILE] + $system_size ) / 1000000 ) > $af_config['sysquota'] ) { header('Location: '.get_settings('siteurl') . "/wp-admin/options-general.php?page=attach-files.php&error=quota"); die(); } } clearstatcache(); $FileTime = filemtime($SrcPathFile); $storedate = date("Y-m-d H:i:s", $FileTime); // File Processing if ( file_exists($SrcPathFile) ) { // Insert meta data into meta table $SQL = "INSERT INTO ".$table_prefix."attached_files_meta ( post_id, datatype, file_name, file_size, file_date ) values ( '$post_id', '$SrcFileType', '$DstFileName', '".filesize($SrcPathFile)."', '$storedate' )"; if ( !$wpdb->query($SQL) ) { die("Failure while inserting file into table!"); } $fileid = $wpdb->insert_id; // Insert into the filedata table $fp = fopen($SrcPathFile, "rb"); while ( !feof($fp) ) { // Make the data mysql insert safe $binarydata = addslashes(fread($fp, 65535)); $SQL = "INSERT INTO ".$table_prefix."attached_files_data ( masterid, file_data ) values ( '$fileid' , '$binarydata' )"; if ( !$wpdb->query($SQL) ) { die("Failure to insert binary inode data row!"); } } fclose($fp); } } $STARTFILE ++; $ONFILE = "af_file" . $STARTFILE; } } /*------------------------------------------------------------- Name: af_delete_fileid Purpose: Remove file database Receive: file id Return: boolean -------------------------------------------------------------*/ function af_delete_fileid ($file_id) { if ( $file_id > 0 ) { global $wpdb, $table_prefix; // Delete Record from meta and data table $SQL = "DELETE FROM ".$table_prefix."attached_files_meta WHERE file_id = '$file_id'"; if ( !$wpdb->query($SQL) ) { die("Failure to delete file meta data from meta table"); } $SQL = "DELETE FROM ".$table_prefix."attached_files_data WHERE masterid = '$file_id'"; if ( !$wpdb->query($SQL) ) { die("Failure to delete file data from data table"); } return TRUE; } } /*------------------------------------------------------------- Name: af_stream_fileid Purpose: Stream file from database Receive: file id Return: boolean -------------------------------------------------------------*/ function af_stream_fileid ($file_id) { global $wpdb, $table_prefix; $nodelist = array(); // Pull file meta-data $SQL = "SELECT * FROM ".$table_prefix."attached_files_meta WHERE file_id = $file_id"; if ( !$file_info = $wpdb->get_row($SQL) ) { die("

Error: File Not Found!

There was a problem retrieving your file from our database. Please try again later or contact the website admin.


".date('m/d/y H:i:s')." URI: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."

"); } // Pull the list of file inodes $SQL = "SELECT id FROM ".$table_prefix."attached_files_data WHERE masterid = $file_id ORDER BY id"; if ( !$file_nodes = $wpdb->get_results($SQL) ) { die("Failure to retrive list of file inodes"); } // Send down the header to the client Header ( "Content-Type: " . $file_info->datatype ); Header ( "Content-Length: " . $file_info->file_size ); Header ( "Content-Disposition: attachment; filename=\"" . $file_info->file_name . "\"" ); // Loop thru and stream the nodes 1 by 1 foreach ( $file_nodes as $node ) { $SQL = "SELECT file_data FROM ".$table_prefix."attached_files_data WHERE id = ".$node->id; if ( !$node_data = $wpdb->get_var($SQL) ) { die("Failure to retrive file node data"); } echo $node_data; } // Record as hit in DB if ( $_GET['record'] != 'no' ) { $SQL = "UPDATE ".$table_prefix."attached_files_meta SET hit_count = hit_count + 1 WHERE file_id = '$file_id'"; $wpdb->query($SQL); } exit(); } /*------------------------------------------------------------- Name: af_display_attachments Purpose: Lists the file(s) attached to post after main content Receive: content Return: content -------------------------------------------------------------*/ function af_display_attachments($content) { global $wp_query, $wpdb, $table_prefix, $af_config; // Get current post ID $post = $wp_query->post; $id = $post->ID; if ( empty($id) AND isset($_GET['post']) ) $id = $_GET['post']; // Load current attachments $SQL = "SELECT * FROM ".$table_prefix."attached_files_meta WHERE post_id = $id ORDER BY file_name ASC"; $results = $wpdb->get_results($SQL); if ( count($results) > 0 ) { // Head of output $content .= '

'.$af_config['heading'].':

'."\n"; // Loop thru attachments foreach ( $results as $file ) { $link = $af_site_url.$af_upload_dir.$file['file_name']; $content .= '

'.$file->file_name.' '.number_format(round(($file->file_size / 1000))).'K

'."\n"; } $content .= "
"; } return $content; } function af_modify_the_form_tag ($buffer) { $buffer = str_replace('

File-Attachments

Use this screen to manage your attached files.

get_results($SQL); $i = 1; // If no files in database if ( count($files) == 0 ) echo ' '."\n"; else { foreach ( $files as $file ) { if ( af_isEven($i) ) $class = ""; else $class = "alternate"; ?> get_var("SELECT COUNT(*) FROM ".$table_prefix."attached_files_meta"); $total_du = $wpdb->get_var("SELECT SUM(file_size) FROM ".$table_prefix."attached_files_meta"); $avg_filesize = $wpdb->get_var("SELECT AVG(file_size) FROM ".$table_prefix."attached_files_meta"); $large_file = $wpdb->get_var("SELECT MAX(file_size) FROM ".$table_prefix."attached_files_meta"); ?>
ID When Post Title Author Filename File Size Downloads
No Files in Database
file_id?> post_date?> post_title?> user_login?> file_name?> file_size / 1000, 0)?> KB hit_count?> View Delete

System Summary

Total No. of Files
Total Disk Usage MB
Average File Size KB
Largest File KB

File-Attachments

Post Heading:
File Restrictions: (Leave blank for no restriction)
Valid Extensions:
Comma separated list of allowed file formats. Ex: (pdf, csv, txt)
Max. Files per Post:
Max. File Size: KB
Cannot exceed server maximum set in the php.ini file. Your server is set to Bytes
Max. Combined File Size per Post: KB
System-wide Disk Usage Restriction: MB

AttachFiles Uninstall

Because AttachFiles creates its own tables in your mysql database for file storage simply disabling the plugin will not delete the files and free up your disk space. To disable the AttachFiles plugin and completely remove the data from your mysql tables you will need to click the button below.

WARNING! -- This process is irreversible!

No other parts of your wordpress installation will be harmed.

Error!

There was a problem with your file upload. The file you were trying to upload exceeds the maximum size limit of KB

Error!

There was a problem with your file upload. The file you are trying to upload exceeds the maximum combined file size per-post limit of KB

Error!

There was a problem with your file upload. This system has a quota set at MB and your file would exceed this amount so it was not allowed. Please clear some space or increase your quota and try again.

query($SQL); $SQL = "DROP TABLE ".$table_prefix."attached_files_data"; $wpdb->query($SQL); // Deactivate Plugin $current = get_settings('active_plugins'); array_splice($current, array_search( 'attach-files.php', $current), 1 ); // Array-fu! update_option('active_plugins', $current); header('Location: plugins.php?deactivate=true'); die(); } ?>