Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting ( E_ALL );
- $allowedExtensions = array (
- 'jpg',
- 'jpeg',
- 'png',
- 'bmp',
- 'tiff',
- 'gif'
- );
- $maxSize = 2097152;
- $dirImage = "photos/tmp_images";
- $errors = $output = array ();
- if (isset ( $_FILES ['image'] )) {
- // Start Validation
- foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
- $fileName = $_FILES ['image'] ['name'] [$key];
- $fileSize = $_FILES ['image'] ['size'] [$key];
- $fileTemp = $_FILES ['image'] ['tmp_name'] [$key];
- $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
- $fileExt = strtolower ( $fileExt );
- if (empty ( $fileName ))
- {
- $errors [$fileName] [] = "Missing File Number $key";
- }
- if (! in_array ( $fileExt, $allowedExtensions )) {
- $errors [$fileName] [] = "format $fileExt in $fileName is not accepted";
- }
- if ($fileSize > $maxSize) {
- $errors [$fileName] [] = "maxsize of 2MB exceeded";
- }
- if (! mkdir_recursive ( $dirImage, 0777 )) {
- $errors [$fileName] [] = "Error Creating /Writing Directory $dirImage ";
- }
- if ($_FILES ['image'] ['error'] [$key] != UPLOAD_ERR_OK) {
- $errors [$fileName] [] = "Upload Errors : " . $_FILES ['image'] ['error'] [$key];
- }
- }
- // Only Stat Uploading if no errors
- if (empty ( $errors )) {
- foreach ( $_FILES ['image'] ['tmp_name'] as $key => $val ) {
- $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
- $filePrifix = basename ( $fileName, "." . $fileExt );
- $i = 0;
- while ( file_exists ( $fileDst ) ) {
- $i ++;
- $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
- }
- // Move the file
- if (count ( $errors ) == 0) {
- if (move_uploaded_file ( $fileTemp, $fileDst )) {
- // ...
- $output [$fileName] = "OK";
- }
- }
- }
- }
- }
- function mkdir_recursive($pathname, $mode) {
- is_dir ( dirname ( $pathname ) ) || mkdir_recursive ( dirname ( $pathname ), $mode );
- return is_dir ( $pathname ) || mkdir ( $pathname, $mode );
- }
- if (! empty ( $errors )) {
- echo "<pre>";
- foreach ( $errors as $file => $error ) {
- echo $file, PHP_EOL;
- echo "==============", PHP_EOL;
- foreach ( $error as $line ) {
- echo $line, PHP_EOL;
- }
- echo PHP_EOL;
- }
- echo "</pre>";
- }
- if (! empty ( $output )) {
- echo "<pre>";
- echo "Uploaded Files", PHP_EOL;
- foreach ( $output as $file => $status ) {
- echo $file, "=", $status, PHP_EOL;
- }
- echo "</pre>";
- }
- ?>
- <form method="post" enctype="multipart/form-data">
- <label for="file">Filename 1:</label> <input type="file" name="image[]"
- id="file" /> <br /> <label for="file">Filename 2:</label> <input
- type="file" name="image[]" id="file" /> <br /> <label for="file">Filename
- 3:</label> <input type="file" name="image[]" id="file" /> <br /> <input
- type="submit" name="submit" value="Submit" />
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement