Rated an average of 4 stars by 2 people

Image Upload

Script Details:
Added on March 14, 2009 by Speedy. Last modified March 14, 2009.
Speedy accreditted the script to himself.

Description:
This function makes imageupload really easy. The info needed will be added in the function parameters

Tags: function, image, upload

Instructions:
// Allowed extensions
$validext = array('jpg','png','gif','mp3','txt','doc','avi');

// Upload destination
$dest = 'image/path/';

// Final foldername (subfolder under image/path/)
$folder = 'final_folder/';

// It not empty, the script will upload the file and rename it to the value of $newname (Will overwrite if exists)
// If empty it will upload the image and keeping it's original name. (Will overwrite if exists)
$newname = 'new_filename';

// Adds the complete $_FILES array into $arr
$arr = $_FILES['file'];

// Set max file size
$maxsize = '10240000';

// If you need to move out of a folder to upload the image, set $offset to the number of folders you
// need to move out of. (I.E: $offset = '3'; will add '../../../' to the beginning of $dest)
$offset = 2;
------------------
now to upload the file this is what you do

ImageUpload($dest,$folder,$arr,$newname,$validext,$maxsize,$offset);

all variables must be set, and only $newname can be empty.

the function also returns true/false, so you can make something like this

$upresult = ImageUpload($dest,$folder,$arr,$newname,$validext,$maxsize,$offset);
if ($upresult) { preform some code }
else { image wasn't uploaded }

Code:

<?php
# Snippet from PHP Share: http://www.phpshare.org

function ImageUpload($dest$folder$arr$newname$validext$maxsize$offset)
{
    if (!empty(
$dest) && !empty($folder) && !empty($arr) && !empty($validext) && !empty($maxsize) && !empty($offset))
    {
        
$ext end(explode('.'$arr['name']));
        
        if (
in_array($ext$validext))
        {
            
$destoff '';
            
            for (
$i 0$i $offset$i++)
            {
                
$destoff .= '../';
            }
            
            
$fullpath $destoff $dest $folder;
            
            if (!
is_dir($fullpath))
            {
                
mkdir($fullpath);
            }
            
            if (
$arr['size'] < $maxsize)
            {
                if (!empty(
$newname))
                {
                    
$file $fullpath '/' $newname '.' $ext;
                }
                else
                {
                    
$file $fullpath $arr['name'];
                }
                
                if (
move_uploaded_file($arr['tmp_name'], $file))
                {
                    return 
true;
                }
                else
                {
                    return 
false;
                }
            }
            else
            {
                return 
false;
            }
        }
        else
        {
            return 
false;
        }
    }
    else
    {
        return 
false;
    }
}
?>

Review this PHP Script:

Rating:

Comments:


Login

User:
Pass:

Register

Not a member? Register today and receive the many free benefits of being a member!

Advertisements