Skip to content

Commit

Permalink
multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-khabbaz committed May 5, 2012
2 parents 0349d69 + a7b48da commit c03664f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
17 changes: 17 additions & 0 deletions [php] file upload/Advanced Multiple Upload/upload.html
@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>Multiple uploading form</title>
</head>
<body>
<h1>Upload Form</h1>
<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="myfile[]" /><br>
<input type="file" name="myfile[]" /><br>
<input type="file" name="myfile[]" /><br>
<!-- MORE AND MORE -->
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
79 changes: 79 additions & 0 deletions [php] file upload/Advanced Multiple Upload/upload.php
@@ -0,0 +1,79 @@
<?php
// Set timezone for probable usage.
date_default_timezone_set('Asia/Tehran');

// Assign valid types
$valid_mime = array(
'image/jpeg',
'image/pjpeg',
'image/jpeg',
'image/png',
'image/gif'
);

function upload($files, $dir, $size_limit=1024, $prevent_duplicate=false){
global $valid_mime;

// $files must be given.
if(!isset($files)) return false;

// Look for $valid_mime array.
isset($valid_mime) and is_array($valid_mime) or die('Error in data resources, valid_mime array not found.');

// Make directory if not exists. set permission to 0777.
is_dir($dir) and chmod($dir, 0777) or mkdir($dir, 0777, true);

$count = 1;
foreach($files as $file){
$file['error'] === UPLOAD_ERR_OK or die('Error in uploading file(s).');

// Check uploaded-file type.
in_array($file['type'], $valid_mime) or die();

// Set size_limit in KB.
$file['size'] > $size_limit*1024 and die('The uploaded file exceeds the maximum file size.');

// Prevent duplicate filenames.
$prefix = ($prevent_duplicate == true) ? time().'_' : '';
$suffix = ($prevent_duplicate == true) ? '_'.$count++ : '';

$file_extension = strrchr($file['name'], '.');
$filename = basename($file['name'], $file_extension);

$file_path = "{$dir}/{$prefix}{$filename}{$suffix}{$file_extension}";

// Move uploaded-file from php temp folder to desire one.
move_uploaded_file($file["tmp_name"], $file_path);

// Make an array of filepaths
$output[] = $file_path;
}

// Change permission of folder according to security issues.
chmod($dir, 0755);

return $output;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Controller Section ////////////////////////////////

// Assign tmp_arr from $_FILES['myfile'] and do die if there is any problem.
$tmp_arr = (isset($_POST['submit']) and isset($_FILES['myfile'])) ? $_FILES['myfile'] : die('Error in posting data.');

// Create an array with desired structure.
for($i=0; $i<count($tmp_arr['name']); $i++){
$files[] = array(
'name' => $tmp_arr['name'][$i],
'type' => $tmp_arr['type'][$i],
'tmp_name' => $tmp_arr['tmp_name'][$i],
'error' => $tmp_arr['error'][$i],
'size' => $tmp_arr['size'][$i],
);
}

// size_limit in KB
$path_arr = upload($files, './public', 1024, true);

// SEE WHAT HAPPENS ;)
echo '<pre>';
var_export($path_arr);
2 changes: 1 addition & 1 deletion [php] file upload/upload.php
Expand Up @@ -35,7 +35,7 @@ function upload_img($dir_path, $size_limit){
return $file_path;
}

$img = upload('./uploaded',200);
$img = upload_img('./uploaded',200);

if($img) echo "Path : $img <br /> <img src='$img' width='300' />";

Expand Down

0 comments on commit c03664f

Please sign in to comment.