Location: PHPKode > scripts > PHP Photo Album > phpalbum/multi_upload/multi_file_upload.php
<?php
    /*
        this sample is proceedural for those not familiar with OOP. Simply include this file in your
        form processing script and it will handle the uploads. You'll definitely want to make changes to the 
        upload directory and to some of the functionality to change it to how you like to work
        
    !!    This file does no security checking - this solely handles file uploads -
    !!    this file does not handle any security functions. Heed that warning! You use this file at your 
    !!    own risk and please do not publically accept files if you don't know what you're doing with
    !!    server security.


        at the end of this script you will have two variables
        $filenames - an array that contains the names of the file uploads that succeeded
        $error - an array of errors that occured while processing files
        
        
        if the max file size in the form is more than what is set in php.ini then an addition 
        needs to be made to the htaccess file to accomodate this
         
        add this to  your .htaccess file for this diretory
        php_value post_max_size 10M
        php_value upload_max_filesize 10M
        
        replace 10M to match the value you entered above for $max_file_size
         
    */    
    //$_FILES['userfile']['name']   name of file including extension
    //$_FILES['userfile']['type']   mime type of the file
    //$_FILES['userfile']['size']   size of file
    
    
    
    // images dir - relative from document root
    // this needs to be a folder that is writeable by the server
//    $image_dir = '/uploads/files/';
//        $upload_dir = '/temp/';

//         $the_path = $_POST['path'];

//         $upload_dir = '/' . $the_path . '/';

    
    // upload dir
//    $destination = $_SERVER['DOCUMENT_ROOT']. '/hallsisland.com/gallery/' . $_POST['path'] . '/';
    $destination = $_SERVER['DOCUMENT_ROOT']. $_POST['gallery_path'] . $_POST['path'] . '/';
        
//echo 'the upload destination is <b>' . $destination . '</b><br><br>';
        
    if(isset($_FILES))
        {
            // initialize error var for processing
            $error = array();     
            
            // acceptable files
            // if array is blank then all file types will be accepted
            /*
            $filetypes = array(
                        'ai' => 'application/postscript',
                        'bin' => 'application/octet-stream',
                        'bmp' => 'image/x-ms-bmp',
                        'css' => 'text/css',
                        'csv' => 'text/plain',
                        'doc' => 'application/msword',
                        'dot' => 'application/msword',
                        'eps' => 'application/postscript',
                        'gif' => 'image/gif',
                        'gz' => 'application/x-gzip',
                        'htm' => 'text/html',
                        'html' => 'text/html',
                        'ico' => 'image/x-icon',
                        'jpg' => 'image/jpeg',
                        'jpe' => 'image/jpeg',
                        'jpeg' => 'image/jpeg',
                        'js' => 'text/javascript',
                        'mov' => 'video/quicktime',
                        'mp3' => 'audio/mpeg',
                        'mp4' => 'video/mp4',                        
                        'mpeg' => 'video/mpeg',
                        'mpg' => 'video/mpeg',
                        'pdf' => 'application/pdf',
                        'png' => 'image/x-png',
                        'pot' => 'application/vnd.ms-powerpoint',
                        'pps' => 'application/vnd.ms-powerpoint',
                        'ppt' => 'application/vnd.ms-powerpoint',
                        'qt' => 'video/quicktime',
                        'ra' => 'audio/x-pn-realaudio',
                        'ram' => 'audio/x-pn-realaudio',
                        'rtf' => 'application/rtf',
                        'swf' => 'application/x-shockwave-flash',
                        'tar' => 'application/x-tar',
                        'tgz' => 'application/x-compressed',
                        'tif' => 'image/tiff',
                        'tiff' => 'image/tiff',
                        'txt' => 'text/plain',
                        'xls' => 'application/vnd.ms-excel',
                        'zip' => 'application/zip'
                    );
                    */
            $filetypes = array(
                        'jpg' => 'image/jpeg',
                        'gif' => 'image/gif'
                        );
                        
            $fileextension = array(

                        'JPG' => '.JPG',
                        'GIF' => '.GIF',
                        'jpg' => '.jpg',
                        'gif' => '.gif'
                        );
            
            //substr_count($text, 'is')
                    
            // function to check for accpetable file type
function okFileType($type)
{
//echo '<br> checking File type of ' . $type;
                    // if filetypes array is empty then let everything through
                    if(count($GLOBALS['filetypes']) < 1)
                        {
                            return true;
                        }
                    // if no match is made to a valid file extension array then kick it back
                    elseif(!in_array($type,$GLOBALS['filetypes']))
                        {
                            $GLOBALS['error'][] = $type.' is not an acceptable file type. <br>'.
                                                  $type.' has been ignored.';
                            return false;
                        }
                        
                    // else - let the file through
                    else
                        {                        
                            return true;
                        }
}



function okFileExtension($name)
{
                     $the_name = strstr($name, '.');
//echo '<br> checking File Extension of ' . $the_name; 
                    // if filetypes array is empty then let everything through
                    if(count($GLOBALS['fileextension']) < 1)
                        {
                            return true;
                        }
                    // if no match is made to a valid file extension array then kick it back
                    elseif(!in_array($the_name,$GLOBALS['fileextension']))
                        {
                            $GLOBALS['error'][] = '<b>' . $name .'</b> is not an acceptable file extension. <br>Possible cause,  extension is uppercase or wrong type.<br>'.
                                                  '<b>' . $name.'</b> has been ignored.';
                            return false;
                        }
                        
                    // else - let the file through
                    else
                        {                        
                            return true;
                        }
}





            
            // function to check and move file
function processFile($file)
{    
//echo '<br>processing file ' . $file['name'] . '  type = ' . $file['type'];

                    // set full path/name of file to be moved
                    $upload_file = $GLOBALS['destination'].$file['name'];
                    
                    if(file_exists($upload_file))
                        {
                            $GLOBALS['error'][] = $file['name'].' - Filename exists - please change your image filename';
                            return false;
                        }
//echo '<br>temp file ' . $file['tmp_name'];
                    if(!move_uploaded_file($file['tmp_name'], $upload_file)) 
                        {
                            // failed to move file
                            $GLOBALS['error'][] = 'File Upload Failed on '.$file['name'].' - Please try again';
                            return false;
                        } 
                    else 
                        {
                        // male the filename lower case  strtolower ( string str )
                        rename ( $upload_file, $GLOBALS['destination'].strtolower ($file['name']) );
                        $upload_file = $GLOBALS['destination'].strtolower ($file['name']);
                            // upload OK - change file permissions
                            chmod($upload_file, 0755);
                            return true;
                        }    
}
                
            // check to make sure files were uploaded
            $no_files = 0;
            $uploaded = array();
            foreach($_FILES as $file)
                {
                
//if($file['name'] != NULL ) echo '<br>processing file  ' . $file['name'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of type = ' . $file['type'];

                    switch($file['error'])
                        {
                            case 0:
                                // file found
                                if($file['name'] != NULL && okFileType($file['type']) != false && okFileExtension($file['name']) != false)
                                    {
                                        // process the file
                                        if(processFile($file) == true)
                                            $uploaded = $file['name'];
//if($file['name'] != NULL ) echo '<br>File ' . $file['name'] . '&nbsp;&nbsp;up-loades sucessfully';
echo '<br>File <b>' . $file['name'] . '</b>&nbsp;&nbsp;up-loaded sucessfully';
                                    }
                                break;
                                
                            case (1|2):
                                // upload too large
                                $error[] = 'file upload is too large for '.$file['name'];
                                break;
                                
                            case 4:
                                // no file uploaded
                                break;
                                
                            case (6|7):
                                // no temp folder or failed write - server config errors
                                $error[] = 'internal error - flog the webmaster on '.$file['name'];
                                break;
                        }
                }
                
        }
        
        
//echo '<br><br>File error count= ' . count($error) . '<br>';

for ($i = 0; $i < count($error); $i++)
{
echo '<br><br><font color="red"><b>Error</b></font> &nbsp;&nbsp;&nbsp;' . $error[$i];

}
echo '<br><br><br><br><form> <input type="button" value="Back to Gallery" onClick="history.go(-2)"></form>';



?> 





Return current item: PHP Photo Album