forked from AliMD/OldWebProgrammingTuts
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters