How To Add Progress Bar in Uploading (PHP) - php

How to add a progress bar on my uploading page?
I will be using A Wordpress template to integrate the uploader.
This is the uploader code I have right now:
global $post;
/*Video Uploading*/
if ( isset($_FILES['upload_attachment']) ) {
$count = '0';
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = betube_insert_attachment($file,$post->ID);
$attachvideo = wp_get_attachment_url( $newupload);
add_post_meta($post_id, 'jtheme_video_file', $attachvideo);
add_post_meta($post_id, '_video_thumbnail', $newupload);
set_post_thumbnail( $post_id, $newupload );
$count++;
}
}
}
}

To add progressbar you must have to upload file with jquery/ajax. There is one default javascript event listener called 'progress' which is used to increment progress bar. Here is a full working example, but make sure as you are working in wordpress so ajax php code will be changed as per your requirement. demo: http://theonlytutorials.com/jquery-ajax-file-upload-with-percentage-progress-bar/

Related

sort by date created php

I have a audio player and its pulling its audio from a folder its currently being sorted by name but I want to sort it by date created. any help would be appreciated.
$media = array();
$di = new DirectoryIterator($dir);
foreach ($di as $fileinfo) {
$path_info = pathinfo($fileinfo->getPathname());
if(isset($path_info['extension'])){
if(in_array(strtolower($path_info['extension']), $allowed_files)){
$fn = $fileinfo->getPathname();
$media[] = array(
"SITE_URL" => SITE_URL,
"SITEPATH" => SITEPATH,
"fullpath" => SITE_URL.'/'.path2url(realpath($path_info['dirname'])).'/'.$path_info['basename'],
"basename" => $path_info['basename'],
"extension" => $path_info['extension'],
"dirname" => realpath($path_info['dirname']),
"filename" => $path_info['filename']
);
}
}
DirectoryIterator only gives access to access Time, modification time or inode change time. If your files have not been changed since creation, modification time will be the same and you can then save that time in the $media array and then sort the array using array_multisort:
$media = array();
$di = new DirectoryIterator($dir);
foreach ($di as $fileinfo) {
$path_info = pathinfo($fileinfo->getPathname());
if(isset($path_info['extension'])){
if(in_array(strtolower($path_info['extension']), $allowed_files)){
$fn = $fileinfo->getPathname();
$media[] = array(
"SITE_URL" => SITE_URL,
"SITEPATH" => SITEPATH,
"fullpath" => SITE_URL.'/'.path2url(realpath($path_info['dirname'])).'/'.$path_info['basename'],
"basename" => $path_info['basename'],
"extension" => $path_info['extension'],
"dirname" => realpath($path_info['dirname']),
"filename" => $path_info['filename'],
"mtime" => $fileinfo->getMTime()
);
}
}
}
array_multisort(array_column($media, 'mtime'), SORT_ASC, $media);

PHP File Upload Restrictions

I want to put a restriction on the size of images uploaded.
Below I have condition for image size.
if ($_FILES["upload_attachment"]["size"] < 25000)) // Max File Size: 25KB
{
echo "File size exceeds maximum.";
}
I want insert condition in this code.
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
}
Basically, you want to check that early, before you proceed with file processing, so you're not making the server do any work if it doesn't have to.
Also, FYI, the upload size is specified in bytes, so if you want 25KB, it will be (25 * 1024) = 25600 bytes.
To incorporate that condition, you could do this. It gets a little gnarly, so I've added comments on the closing brackets to note which closes what. fe = foreach
if ( $_FILES ) {//if files exist
if ($_FILES["upload_attachment"]["size"] < 25600) { // Max File Size: 25KB
//I would also recommend adding error handling early.
if ($_FILES['filename']['error'] > 0){
echo 'Error: ' . $_FILES['filename']['error'] . '<br />';
} else {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}//fe
}//if
}//fe
}//if no errors (else)
} //if size ok
else {
echo "File size exceeds maximum.";
}
}//if $_FILES

Form an array hierarchy from specifically-formatted strings

I'm iterating over a folder and formatting its contents in a certain way.
I've to form an array from this set of strings:
home--lists--country--create_a_country.jpg
home--lists--country--list_countries.jpg
profile--edit--account.jpg
profile--my_account.jpg
shop--orders--list_orders.jpg
The array needs to look like this:
<?php
array(
'home' => array(
'lists' => array(
'country' => array(
'create_a_country.jpg',
'list_countries.jpg'
)
)
),
'profile' => array(
'edit' => array(
'account.jpg'
),
'my_account.jpg'
),
'shop' => array(
'orders' => array(
'list_orders.jpg',
)
);
The thing is, the depth of the array could be infinitely deep depending on how many '--' dividers the file name has. Here's what I've tried (assuming each string is coming from an array:
$master_array = array();
foreach($files as $file)
{
// Get the extension
$file_bits = explode(".", $file);
$file_ext = strtolower(array_pop($file_bits));
$file_name_long = implode(".", $file_bits);
// Divide the filename by '--'
$file_name_bits = explode("--", $file_name_long);
// Set the file name
$file_name = array_pop($file_name_bits).".".$file_ext;
// Grab the depth and the folder name
foreach($file_name_bits as $depth => $folder)
{
// Create sub-arrays as the folder structure goes down with the depth
// If the sub-array already exists, don't recreate it
// Place $file_name in the lowest sub-array
// .. I'm lost
}
}
Can anyone shed some light on how I might do this? All insight appreciated.
w001y
Try this:
$files=array("home--lists--country--create_a_country.jpg","home--lists--country--list_countries.jpg","profile--edit--account.jpg","profile--my_account.jpg","shop--orders--list_orders.jpg");
$master_array=array();
foreach($files as $file)
{
$file=explode("--",$file);
$cache=end($file);
while($level=prev($file))
{
$cache=array($level=>$cache);
}
$master_array=array_merge_recursive($master_array,$cache);
}
print_r($master_array);
Live demo

Codeigniter 2.1 - fileupload You did not select a file to upload

Model function:
public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
$folder = $this->path . $folder;
$files = array();
$count = 0;
foreach ($_FILES as $key => $value) :
$file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
$file_name = $this->global_functions->char_replace($file_name, '_');
$count++;
$config = array(
'allowed_types' => $allowed_type,
'upload_path' => $folder,
'file_name' => $file_name,
'max_size' => $max_size,
'max_width' => $max_width,
'max_height' => $max_height,
'remove_spaces' => TRUE
);
$this->load->library('image_lib');
$this->image_lib->clear();
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) :
$error = array('error' => $this->upload->display_errors());
var_dump($error);
return FALSE;
else :
$file = $this->upload->data();
$files[] = $file['file_name'];
endif;
endforeach;
if(empty($files)):
return FALSE;
else:
return implode(',', $files);
endif;
}
This function is working partially. Files are being uploaded to the selected folder, but I am getting this error: You did not select a file to upload. and getting FALSE as a result? What seems to be a problem? (form is using form_open_multipart)
Please make sure that you have this name of your file tag field:
$key='userfile' //which is name of input type field name
Are any of your file inputs empty when this happens? $_FILES will contain an array of "empty" data if there is no file specified for the input. For example, my file input name is one, and I submitted it without specifying a file:
Array
(
[one] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
You should check if the file data is empty before processing the upload. PHP's is_uploaded_file() is useful for this.

The name doesnt change but the path does in the array

So I am having issues understanding how to iterate over an array that looks as such in php:
$styles = array(
'css' => array(
'name' => array(
'core-css',
'bootstrap-css',
'bootstrap-responsive-css'
),
'path' => array(
get_bloginfo('stylesheet_url'),
get_template_directory_uri() . '/lib/bootstrap/css/bootstrap.min.css',
get_template_directory_uri() . '/lib/bootstrap/css/bootstrap.responsive.min.css'
),
),
);
Essentially this styles gets passed to a constructor of a class which then iterates over the array in a method that looks like this (note this array is stored class level in a protected value called _options, hence the $this->_options in the following code:
foreach ( $this->_options as $key => $value ) {
// load all the css files
if (isset ( $this->_options ['css'] )) {
foreach ( $value ['name'] as $name ) {
foreach ( $value ['path'] as $path ) {
wp_enqueue_style ( $name, $path );
}
}
}
}
This will spit out something like:
core-css style.css
core-css bootstrap
core-css bootstrap-responsive
.
The problem should be clear right now, the name never changes and I think it has something to do with how I am iterating over the array of, essentially, arrays.
So your help is much appreciated.
The path array is not apart of the name array, and should look something like:
foreach ( $this->_options as $key => $value ) {
// load all the css files
if (isset ( $this->_options ['css'] )) {
foreach ( $value ['path'] as $k=>$path ) {
wp_enqueue_style ( $value['name'][$k], $path );
}
}
}
Or, you could always change the structure of your array:
$styles = array(
'css' => array(
array(
'name'=>'core-css',
'path'=>get_bloginfo('stylesheet_url')
),
array(
'name'=>'bootstrap-css',
'path'=>get_template_directory_uri() . '/lib/bootstrap/css/bootstrap.min.css'
),
array(
'name'=>'bootstrap-responsive-css',
'path'=>get_template_directory_uri() . '/lib/bootstrap/css/bootstrap.responsive.min.css'
)
)
);
foreach($this->_options as $key => $value){
if (isset ( $this->_options ['css'] )) {
foreach($this->_options ['css'] as $k=>$v){
wp_enqueue_style ( $v['name'], $v['path'] );
}
}
}
Hope that order matches, other way you in trouble.
Something like this:
if (isset($this->_options['css'])) {
foreach ($this->_options['css']['name'] as $key => $name) {
echo $name; // Your name
echo $this->_options['css']['path'][$key]; // Your math
}
}

Categories