Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-29-2012, 08:40 PM   PM User | #1
Chris-2k
Regular Coder

 
Join Date: May 2011
Posts: 303
Thanks: 29
Thanked 0 Times in 0 Posts
Chris-2k is an unknown quantity at this point
Multi-upload Script

Save this file as index.php, upload it n test......

PHP Code:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
</head>

<body>

<form enctype="multipart/form-data"  action="" method="POST">

    <input name='image_upload[]' type='file'  multiple=20 /><br>
    <input name='image_upload[]' type='file'  multiple=20 /><br>
    <input name='image_upload[]' type='file'  multiple=20 /><br>
    
    <input type="submit" value="Send File" />
</form> 

<?php
    $tFiles 
$_FILES['image_upload']['tmp_name'];
    
print_r($_FILES);
    
    for(
$i 0$i count($tFiles); ++$i)
    {
        
performaLocalUpload($_FILES['image_upload']['tmp_name'][$i], $uploadDir$allowed$maxfileSize);
    }

function 
performaLocalUpload($files$uploadDir$allowed$maxfileSize)
{
    
$uploadDir $_SERVER['DOCUMENT_ROOT'] .'uploads';    
    
    if(
$ext checkitsaValidImage($files))
    {
        
$new_name gen_uniqueFilename() . $ext// we will give an unique name.
        
$uploadFilePath $uploadDir .'/'$new_name;

        if (!
move_uploaded_file($files$uploadFilePath)) {
            
trigger_error('Failed to upload image');
        }
    }
}

function 
checkitsaValidImage($files
{
    include 
$_SERVER['DOCUMENT_ROOT'] .'/config.php';
    
$imageInfo getimagesize($files);
    
$size filesize($files);
    
$sResult '';
        
    if(!
$imageInfo) {
        
trigger_error('Error not an image.');
    }
        
    if(!empty(
$imageInfo) && !in_array($imageInfo['mime'], $allowed)) {
        
trigger_error('Error invalid file.');
    }
            
    else if(
$imageInfo[0] > $maxWidth || $imageInfo[1] > $maxHeight) {
        
trigger_error('Error image size too  big.');
    }
    else if(
$size $maxfileSize) {
        
trigger_error('Error file size too big.');
    }
    else
    {
        
$sResult image_type_to_extension($imageInfo[2], true);
    }
    return 
$sResult;
}
?>
</body>
</html>
This script works 100%, you need to make 3 vars for checkitsanImage() function, go figure..

Credits: me (Chris-2k)
Chris-2k is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:01 PM.


Advertisement
Log in to turn off these ads.