Zend - The PHP Company




File Uploading

Add Code


upload controler by thephpguy.com  

Type: code fragment
Added by: thephpguy
Entered: 24/04/2005
Last modified: 02/12/2005
Rating: - (fewer than 3 votes)
Views: 3834
With this script you can easily control: 1. What extentions can be uploaded. 2. the size of the file 3. (only applies to images) Set a max or min. height and width 4.Change the name of the file to what you want. (i.e. username) 5. Change the extention of the file.(you may wish to do this for security reasons say you allow .php files to be uploaded, you could turn evil.php into evil.nullified)


<?PHP 
/****************************************************** 
** 
**              This script was written by: 
**                    Timothy Martens 
** 
**      Use this script all you want, all I ask is 
**            you leave this header intact 
** 
**                   ThePHPGuy.net 
******************************************************/ 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Untitled Document</title> 
</head> 

<body> 
<center> 
    If you do not see a "Browse" button you must be <br /> 
using an older browser that does not support the upload function.<br /> 
to get a updated browser FREE go to www.mozilla.com <br /> 
<br /> 
<br /> 
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="post" ENCTYPE="multipart/form-data"> 
File:<input type="file" name="userfile" size="30"> <input type="submit" value="Upload" name="submit"  > 
</form> 

<?php 
// See if the form has been submitted! 
if (!isset($_POST['submit'])){/* Do nothing until the form is submited */

// 
// form submitted 
// 
else {  // Let's roll 


/********************* 
*                    * 
*        Config       * 
*                    * 
*********************/ 
$newfilename substr(md5(time()),0,10); // This creates a semi random code for the file name. Change to whatever you want just note that two files with the same name will be ovewritten 
$uploadpath "./fresno/cmartens/bob"// Directorie to upload files to. NOTE: Dir May need to be Chmoded 0777 But as that could be dangerous try 0766 first | you can find a chmod calculator @ http://www.thephpguy.net/tools/chmod.php 
$good_extensions "jpg|gif|png"// Good extentions!!! Do not put extensions such as .swf or .zip in here if you gave them execute permission to the dir. 
$max_size "500000000"// Max size of file in bytes 
$max_height ""// Max height in pixels || Leave blank for no max height 
$max_width "";  // Max width in pixels  || Leave blank for no max width 
$min_height ""// Leave blank for no min height 
$min_width "";// Leave blank for no min width 
/////////////////////////////////////////////////////// 

// See if the $uploadpath exists 
if(is_dir($uploadpath)){} 
else {
/* mkdir("$uploadpath", 0766); only use this if you only have one dir in the $uploadpath */ echo "upload path dir does not exsist"; exit; } 

$extension_path pathinfo($_FILES['userfile']['name']); 
$extension $extension_path[extension]; 
$explode_extensions explode("|"$good_extensions); 

for(
$i 0$i count($explode_extensions); $i++) 
    { 
        if (
$explode_extensions[$i] == "$extension") {$ext_good "1";} 
    } 
    echo 
"<br>The extension is $extension<br>"

///////////////////////////////////////////////////// 
// Check File Size 
if ($ext_good == "1") { 
if(
$_FILES['userfile']['size'] > $max_size

echo 
"File size is too big!"
exit; 


// Check Height & Width 
if ($max_width == "" && $max_height == "") {} 
else{ 
if (
$max_width && $max_height) { 
list(
$width$height$type$w) = getimagesize($_FILES['userfile']['tmp_name']); 
if(
$width $max_width){ 
echo 
"The file width is too large!<br><br>The images max width is $max_width pixels"// To Wide 
exit;} 
if (
$height $max_height){ 
echo 
"The file height is too large!<br><br>The images max height is $max_height pixels";// To Tall 
exit;} 
// end else 
// end if 

if($width $min_width){ 
echo 
"The file width is too small!<br><br>The images min width is $min_width pixels"// Not wide enough 
exit;} 
if (
$height $min_height){ 
echo 
"The file height is too small!<br><br>The images min height is $min_height pixels";// To short 
exit;} 


// The Upload Part 
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) 
    { 
        
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadpath.'/'."$newfilename"."."."$extension") or die("Sorry some error occured we were not able to upload the file.");// renames file to user ID + . + OriginalExtention 
    


print 
"Your Picture has been successfully uploaded."

// End if "file is '$ext_good'" 
    
else 
        { 
            print 
"Sorry incorrect file extension! <br><br><br>Try a jpg or a gif =)"
        } 
//end of 'if submit' 
?> 
</center> 
</body> 
</html> 


Usage Example




Rate This Script





Search



This Category All Categories