Uploading csv file from frontend giving error - wordpress

$imgurl = $_FILES['file'];
$pid = $_POST['pid'];
//print_r($imgurl);
if (!empty($imgurl)) {
$uploadme = wp_upload_dir();
if (!function_exists('wp_handle_upload')) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($imgurl, $upload_overrides);
print_r($movefile);
Hi, Above is the code i am trying to upload an csv file into the database in wordpress. everything is good as far as image is concerned but when i am trying to upload the .csv file then i am receiving an error.
[error] => Sorry, this file type is not permitted for security reasons.
Please help.

Add this to your functions.php:
<?php
function cc_mime_types($mimes) {
$mimes['csv'] = 'application/octet-stream';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
?>

Related

How to create a folder inside uploads folder and store uploaded files there?

I want uploaded files to be stored in wp-content/uploads/example/
How to edit this code so this can be done?
$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '/' .
basename($_FILES["fileToUpload"]["name"]);
Right now, it uploads files to wp-content/uploads/2018/10
I tried:
$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '../../example' .
basename($_FILES["fileToUpload"]["name"]);
but it gives me file (uploadedfile.ext) uploaded in wp-content/uploads/2018 as exampleuploadedfile.ext
Instead of this code
$target_dir = wp_upload_dir();
$target_file = $target_dir['path'] . '../../example' .
Try this code. This code will change your upload directory path.
I have used this to upload file in my custom folder in Upload Directory.
$target_dir = wp_upload_dir();
$target_file = $target_dir['basedir'].'/example'.basename($_FILES["fileToUpload"]["name"]);
Solution:
public function onixion_custom_dir_upload_function( $param ){ $mydir = '../../../example'; $param['path'] = $param['path'] . $mydir; $param['url'] = $param['url']
. $mydir; return $param; }
add_filter( 'upload_dir', array( $this, 'onixion_custom_dir_upload_function' )
);
It creates a folder example in the uploads folder, and stores uploaded files there.
If you're not in a class, you don't need
array( $this, 'onixion_custom_dir_upload_function' )
just write:
add_filter( 'upload_dir', 'onixion_custom_dir_upload_function');
If someone have same needs.

Add files programmatically to Order in Woocommerce

I'm using the "woocommerce_order_status_completed" hook to create a file dynamically after the user has paid for the order.
I need to add this file to his downloadable area in that order in this hooks.
Any ideias how to attach a file to a order?
woocommerce_order_status_completed you can add below code...
First store the file you have created in uploads using media_handle_upload
if($_FILES){
//if u don't want to $post_id u gan give 0
$attachment_id = media_handle_upload( 'abe_update_epub', $post_id );
if ( is_wp_error( $attachment_id ) ) {
$errors = $attachment_id->get_error_messages();
foreach( $errors as $error ){
echo $error;
}
echo 'There was an error uploading the image';
} else {
// NEW FILE: Setting the name, getting the url and and Md5 hash number
$file_name = 'Epub Files';
$file_url = wp_get_attachment_url($attachment_id);
$md5_num = md5( $file_url );
// Inserting new file in downloadable files
$files[$md5_num] = array(
'name' => $file_name,
'file' => $file_url
);
// Updating database with the new array
$order = new WC_Order($order_id);
if(!empty($files)){
update_post_meta($order->ID,_files,$files));
}
// Displaying a success notice
echo 'The image was uploaded successfully!';
}
}
Hopes this help u..

Wordpress wp_redirect issue

I am having some trouble with wp_redirect in my plugin.
Here is my code:
public function update($year, $edit_id, $column1, $column2) {
global $wpdb;
$table_name = $wpdb->prefix . 'tableName_'.$year;
$Column1 = intval($column1);
$Column2 = intval($column2);
$NewTotal = ($Column1 + $Column2);
$wpdb->update($table_name, array('Column1' => $Column1, 'Column2' => $Column2, 'Column3' => $NewTotal), array('month_id' => $edit_id));
wp_redirect(get_option('siteurl').'/wp-admin/admin.php?page=myplugin');
exit();
}
When I execute:
I get the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/.../public_html/wp-admin/includes/template.php:1953) in /home/.../public_html/wp-includes/pluggable.php on line 1171
Any help would be appreciated.
Thanks
Can you please add below code in your functions.php
function app_output_buffer() {
ob_start();
} // soi_output_buffer
add_action('init', 'app_output_buffer');

unable to obtain buddypress user avatar url

I am working on a plugin, inside the plugin, I have made a Javascript file in which I want to have the specific user BuddyPress avatar URL assigned to a variable.
My javascript file inside the plugin folder is named: myscript.js.php (so PHP can be executed inside this file).
The code inside the javascript file:
<?php
header('Content-type: text/javascript');
$home_dir = preg_replace('^wp-content/plugins/[a-z0-9\-/]+^', '', getcwd());
include($home_dir . 'wp-load.php');
$ucurrentid = $current_user->ID;
$member_id = bp_core_get_userid( $ucurrentid );
$uphoto = bp_core_fetch_avatar ( array(
'item_id' =>$ member_id,'html'=>false
) );
?>
var uid = <?php echo $current_user->ID ?>;
var uphoto = <?php echo $uphoto ?>;
alert(uphoto);
This return the error :
PHP Fatal error: Call to undefined function bp_user_avatar()
Why is the function undefined, which BuddyPress file should I include into the code so the function works?
This works for me:
http://viviendoenlaeradelaweb20.blogspot.com/2013/03/buddypress-avatar-url.html
I hope will be useful :-)
You can try :
$avatar_link = bp_core_fetch_avatar(array('html' => false, 'item_id' => $user_id));
$avatar = '<img title="admin" src="' . $avatar_link . '"></img>';
// Display image
echo $avatar
You need user ID. Inside loop, you can do :
$user_id = bp_get_member_user_id();

changing location of uploads folder for custom post type only - not working

I'm using a snippet of code in my functions.php which should in theory change the location of my uploads for my custom post type (leaving the pages and posts upload directory the same)
My custom post type is 'download'
and my new directory is a folder called 'downloads' in my 'wp-content' folder.
My wp-content directory looks like this...
wp-content
downloads
plugins
themes
uploads
See function below, can any help me understand why this is not working? Thanks.
add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ){
global $post;
if ( $post->post_type != 'download' ) {
return $default_dir;
}
/*
* On success, the returned array will have many indices:
* 'path' - base directory and sub directory or full path to upload directory.
* 'url' - base url and sub directory or absolute URL to upload directory.
* 'subdir' - sub directory if uploads use year/month folders option is on.
* 'basedir' - path without subdir.
* 'baseurl' - URL path without subdir.
* 'error' - set to false.
*/
// Adjust settings here
$bdir = 'wp-content';
$subdir = date( 'Y/m' );
$dir = $bdir . $subdir;
$burl = content_url('downloads');
$url = $burl . $subdir;
$custom_dir = array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl
);
return shortcode_atts( $custom_dir, $default_dir );
}
The uploads_dir filter needs another implementation to successfully modifying the uploads dir/path.
add_filter('wp_handle_upload_prefilter', 'so_8519968_handle_upload_prefilter');
add_filter('wp_handle_upload', 'so_8519968_handle_upload');
function so_8519968_handle_upload_prefilter( $file )
{
add_filter('upload_dir', 'so_8519968_custom_upload_dir');
return $file;
}
function so_8519968_handle_upload( $fileinfo )
{
remove_filter('upload_dir', 'so_8519968_custom_upload_dir');
return $fileinfo;
}
function so_8519968_custom_upload_dir( $path )
{
// Check if uploading from inside a post/page/cpt - if not, default Upload folder is used
$use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false;
if( !empty( $path['error'] ) || $use_default_dir )
return $path;
// Check if correct post type
$the_post_type = get_post_type( $_REQUEST['post_id'] );
if( 'movies' != $the_post_type )
return $path;
$customdir = '/' . date( 'Y/m' );
//remove default subdir (year/month) and add custom dir INSIDE THE DEFAULT UPLOAD DIR
$path['path'] = str_replace( $path['subdir'], '/downloads' . $customdir, $path['path']);
$path['url'] = str_replace( $path['subdir'], '/downloads' . $customdir, $path['url']);
$path['subdir'] = $customdir;
return $path;
}
The caveat here is that the structure is /wp-content/uploads/downloads/yyyy/mm/filename.ext.
I thought the following would handle /wp-content/downloads/... but am not being able to determine why it is not working.
$path['path'] = str_replace( 'uploads' . $path['subdir'], 'downloads' . $customdir, $path['path']);
$path['url'] = str_replace( 'uploads' . $path['subdir'], 'downloads' . $customdir, $path['url']);
$path['basedir'] = str_replace( 'uploads', 'downloads', $path['basedir']);
$path['baseurl'] = str_replace( 'uploads', 'downloads', $path['baseurl']);
$path['subdir'] = $customdir;
Maybe the uploads folder must be set to wp-content in Media Settings, http://example.com/wp-admin/options-media.php, and make the first two conditionals of the function so_8519968_custom_upload_dir deal with the other upload possibilities (direct in Media Library -post without ID, and in another post types).

Resources