ianhaney
01-24-2016, 09:11 PM
Hi
Sorry got a new issue on another project I am working on
it's a file upload project that has the multiple files upload feature stored on the server in a folder and a select and delete feature as well but the progress bar part is not working that shows the user the progress as a percentage for their uploaded files
I think the js is conflicting with the delete image part but not 100%, below is the scripts
memberpage.php
<div class="status"></div>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<input type="file" id="files" name="files[]" multiple="multiple" required="required" />
<input type="submit" value="Upload!" />
</form>
<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>
<h1>Your Images</h1>
<form method="post" action="deletepho.php">
<center>
<input type="submit" value="Delete" name="Delete">
</center>
<?php
//lets assign the folder name in $title
//You can assign any name
$title= "user-images";
/*$directory corresponds to whole path. Edit to your preference. (i assume u store your
images in directory named "images")*/
$directory = "user-images";
//The array specifies the format of the files
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0)
$nomargin='nomargin';
echo'
<div id="picture">
<img src="'.$directory.'/'.$file.'">
<br>
Select <input type="checkbox" value="'.$directory.'/'.$file.'" name="imgfile[]">
</div>';
}
$i++;
}
closedir($dir_handle);
//Now we have the list of images within form elements
?>
</form>
uploader.php script
<?php
//define page title
$title = 'Uploaded Successful';
//include header template
require('layout/header.php');
$max_size = 1024*200;
$extensions = array('jpeg', 'jpg', 'png', 'gif');
$dir = 'user-images/';
$count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
// loop all files
foreach ( $_FILES['files']['name'] as $i => $name )
{
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['files']['tmp_name'][$i]) )
continue;
// skip large files
if ( $_FILES['files']['size'][$i] >= $max_size )
continue;
// skip unprotected files
if( !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensions) )
continue;
// now we can move uploaded files
if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) )
$count++;
}
echo json_encode(array('count' => $count));
}
//include header template
require('layout/footer.php');
?>
script.js - FOR THE PROGRESS BAR PART
$ (function() {
/* variables */
var status = $('.status');
var percent = $('.percent');
var bar = $('.bar');
/* submit form with ajax request using jQuery.form plugin */
$('form').ajaxForm({
/* set data type json */
dataType:'json',
/* reset before submitting */
beforeSend: function() {
status.fadeOut();
bar.width('0%');
percent.html('0%');
},
/* progress bar call back*/
uploadProgress: function(event, position, total, percentComplete) {
var pVel = percentComplete + '%';
bar.width(pVel);
percent.html(pVel);
},
/* complete call back */
complete: function(data) {
status.html(data.responseJSON.count + ' Files uploaded!').fadeIn();
}
});
});
deletepho.php
<?php
//define page title
$title = 'Successfully Deleted Photo(s)';
//include header template
require('layout/header.php');
$file = $_REQUEST['imgfile'];
$num_files=count($file);
for($i=0;$i<$num_files;$i++)
{
unlink ("$file[$i]");
}
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">';
echo '<br><br>';
echo 'Image(s) successfully deleted.';
echo '<hr>';
echo '<a href="memberpage.php">back to profile page</a>';
echo '</div>';
echo '</div>';
echo '</div>';
//include header template
require('layout/footer.php');
?>
Sorry got a new issue on another project I am working on
it's a file upload project that has the multiple files upload feature stored on the server in a folder and a select and delete feature as well but the progress bar part is not working that shows the user the progress as a percentage for their uploaded files
I think the js is conflicting with the delete image part but not 100%, below is the scripts
memberpage.php
<div class="status"></div>
<form action="uploader.php" method="post" enctype="multipart/form-data">
<input type="file" id="files" name="files[]" multiple="multiple" required="required" />
<input type="submit" value="Upload!" />
</form>
<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>
<h1>Your Images</h1>
<form method="post" action="deletepho.php">
<center>
<input type="submit" value="Delete" name="Delete">
</center>
<?php
//lets assign the folder name in $title
//You can assign any name
$title= "user-images";
/*$directory corresponds to whole path. Edit to your preference. (i assume u store your
images in directory named "images")*/
$directory = "user-images";
//The array specifies the format of the files
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0)
$nomargin='nomargin';
echo'
<div id="picture">
<img src="'.$directory.'/'.$file.'">
<br>
Select <input type="checkbox" value="'.$directory.'/'.$file.'" name="imgfile[]">
</div>';
}
$i++;
}
closedir($dir_handle);
//Now we have the list of images within form elements
?>
</form>
uploader.php script
<?php
//define page title
$title = 'Uploaded Successful';
//include header template
require('layout/header.php');
$max_size = 1024*200;
$extensions = array('jpeg', 'jpg', 'png', 'gif');
$dir = 'user-images/';
$count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
// loop all files
foreach ( $_FILES['files']['name'] as $i => $name )
{
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['files']['tmp_name'][$i]) )
continue;
// skip large files
if ( $_FILES['files']['size'][$i] >= $max_size )
continue;
// skip unprotected files
if( !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensions) )
continue;
// now we can move uploaded files
if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) )
$count++;
}
echo json_encode(array('count' => $count));
}
//include header template
require('layout/footer.php');
?>
script.js - FOR THE PROGRESS BAR PART
$ (function() {
/* variables */
var status = $('.status');
var percent = $('.percent');
var bar = $('.bar');
/* submit form with ajax request using jQuery.form plugin */
$('form').ajaxForm({
/* set data type json */
dataType:'json',
/* reset before submitting */
beforeSend: function() {
status.fadeOut();
bar.width('0%');
percent.html('0%');
},
/* progress bar call back*/
uploadProgress: function(event, position, total, percentComplete) {
var pVel = percentComplete + '%';
bar.width(pVel);
percent.html(pVel);
},
/* complete call back */
complete: function(data) {
status.html(data.responseJSON.count + ' Files uploaded!').fadeIn();
}
});
});
deletepho.php
<?php
//define page title
$title = 'Successfully Deleted Photo(s)';
//include header template
require('layout/header.php');
$file = $_REQUEST['imgfile'];
$num_files=count($file);
for($i=0;$i<$num_files;$i++)
{
unlink ("$file[$i]");
}
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">';
echo '<br><br>';
echo 'Image(s) successfully deleted.';
echo '<hr>';
echo '<a href="memberpage.php">back to profile page</a>';
echo '</div>';
echo '</div>';
echo '</div>';
//include header template
require('layout/footer.php');
?>