News:

Wondering if this will always be free?  See why free is better.

Main Menu

Code Review Needed

Started by The Wizard, December 15, 2012, 04:23:21 PM

Previous topic - Next topic

The Wizard

Please review the code below and if you spot any problems / issues please let me know. The code will be used in a mod so I need to have it up to SMF mod standards.

Note: I know the error text needs to be converted into $txt. I just let them in there for easy reading and understanding of the code at this moment.

Thanks

Wizard


Function wizard_add_item ()
{
global $smcFunc, $context, $sourcedir;

loadTemplate('wizard_add_item_step1');

    $upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

$enable_thumbnails = 1 ;

    define ("MAX_SIZE","500");

function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
    $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
return $ext;
}

    $errors=0;

if(isset($_POST['Submit'])) {
        $image=$_FILES['image']['name'];

if ($image) {
    $filename = stripslashes($_FILES['image']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);

if (($extension != "png")) {
   
    $errors=1;
    $enable_thumbnails = 0 ;
$context['wizard_error_file_extension'] =  '<br />You used a Image File extension that is not a png.<br />Unable to proceed.';

} else {

        $size=filesize($_FILES['image']['tmp_name']);

    if ($size > MAX_SIZE*500){
        $errors=1;
$context['wizard_error_size'] =  '<br />You have exceeded the size limit!<br />Copy unsuccessfull!';
    }

    $newname= $upload_dir.$image;

if(isset($_POST['Submit']) && !$errors) {

    $copied = copy($_FILES['image']['tmp_name'], $newname);

    } else {

$errors=1;
}}}}

// Thumbnail Creator

    $img_rname = $image;

if(isset($_POST['Submit']) && !$errors) {

    global $smcFunc, $context, $sourcedir, $boardurl;

function make_thumbnails($updir, $img)
{

    global $smcFunc, $sourcedir;

    $thumbnail_width = 45;
$thumbnail_height = 45;

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image, $sourcedir . "/shop/Wizards_Gallery/small/" . "$img");
}

} // End of Thumbnail Creator

    if($enable_thumbnails = 1) make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";
$context['wizard_thumb_image'] = $boardurl . "/Sources/shop/Wizards_Gallery/small/" . $img_rname;
$context['wizard_uploaded_successfully'] = '<p>'.$feedback.'</p>File Uploaded Successfully!';

// Proceed to Step 2
   
        $context['wizard_Step2'] = $boardurl . "/index.php?action=admin;area=wizard_add_item_step2";


}

} // End of Function wizard bracket

Colin

The indentation looks a bit off in some cases.
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

SA™

line 111
should use $scriptur instead of $boardurl . "/index.php
line 106
should use  $sourcedir instead of $boardurl . "/Sources
line 70
remove uneeded global $smcFunc from make_thumbnails function
line 65
remove undeeded global $smcFunc
line 5
remove undeeded globals $smcFunc and $context
http://samods.github.io/SAChatBar/

Xbox Live: smokerthecheese 360 or xbone
My Work
Piano Movers / Delivery service
QuoteMy allies are dead.
I'm 'bout to be too.
Zombies are chasing me.
F*** it, I'm screwed -___-

NanoSector

Also don't put functions in functions.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

IchBin™

I say separate functions definitely. Look at how SMF does the code. Does SMF put functions inside of if statements? All functions are separated.

There are several lines that you can combine to make things shorter.
For example:
    $extension = getExtension($filename);
    $extension = strtolower($extension);

Could be:
    $extension = strtolower(getExtension($filename));

I do believe you can get the file size from the $_FILES array too.
$_FILES['image']['size'] instead of using filesize()

Lot's of formatting issues. I'd say you should look at SMF formatting too, do your code the same way if you want it to be up to par with SMf.
IchBin™        TinyPortal

NanoSector

Also,

define ("MAX_SIZE","500");
why not make a variable of this?
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Shambles

I'd just add, that for future support you should comment the code a little more. Might be easy for you now to know what's going on, and why the code is doing what it's doing, but the next guy along might struggle, without pointers in the code...

Kays

#7
All text which is displayed needs to be converted to $txt strings and added to the language file(s).

So something like this:


$context['wizard_error_size'] =  'You have exceeded the size limit!<br />Copy unsuccessfull!';


Should be changed to something like:


$context['wizard_error_size'] =  $txt['wiz_size_error'];


With the following added to the language file;


$txt['wiz_size_error'] = '<br />You have exceeded the size limit!<br />Copy unsuccessfull!';






    $copied = copy($_FILES['image']['tmp_name'], $newname);


When uploading files use the move_uploaded_file() function rather than copy(). move_uploaded_file() will move the file from the temp folder and verify that it is an upload file.

After the file is moved to the attachment folder do a getimagesize() on and verify the MIME type. This will also verify that it's a valid image file.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

The Wizard

Hello:

Thank you all for looking at my code and for all the wonderful suggestions. Below is the latest updated code with all your suggestions. If you see anything else I need to fix / change please let me know.

Thanks

Wiz

Note: Some of you have commented about my indentation. The simple fact is I don't know about the line ups as  I have never learned this step so I'm going with my own judgment and the fact I like straight lines. - wiz


if (!defined('SMF'))
die('Hacking attempt...');

Function wizard_add_item ()
{

global $sourcedir, $context, $boardurl, $scripturl, $txt;

loadTemplate('wizard_add_item_step1');

    // add to language file - just here for testing
    $txt['wiz_file_extension_error'] =  '<br />You used a Image File extension that is not a png.<br />Unable to proceed.';
    $txt['wiz_size_error'] = '<br />You have exceeded the size limit!<br />Copy unsuccessfull!';
    $txt['wiz_general_error'] = '<br />Unable to upload your image for some unknown reason<br />Please check image and server';
   
// path to directory where big uploaded image goes

        $upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

    $image=$_FILES['image']['name'];

    $newname = $upload_dir.$image;

    $img_rname = $image;

    // error checking system set to zero
    $errors=0;

// Maximum Size a image can be
        $max_file_size = 500;
   
    // get the file exention of the uploaded image
      $extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
       
    // check and make sure the file exntion is only "png" if not do not upload and send error message   
    if(isset($_POST['Submit']) && $extension != "png") {
    $errors=1;
$context['wizard_error_file_extension'] =  $txt['wiz_file_extension_error'];
} else {
    define ("MAX_SIZE","$max_file_size");
    $size=filesize($_FILES['image']['tmp_name']);
}

// check and make sure file size is not over limit if so do not upload and send error message 
if ($size > MAX_SIZE*$max_file_size){
        $errors=1;
$context['wizard_error_size'] =  $txt['wiz_size_error'];
    }

// If there are no errors the upload image to big file if not do not upload and send error message
// I did try move_uploaded_file() instead of copy() but move_uploaded_file() slowed down my server so I opted for copy()
if(isset($_POST['Submit']) && !$errors) {
   $copied = copy($_FILES['image']['tmp_name'], $newname);
    } else {
   $errors=1;
   $context['wizard_error_general'] =  $txt['wiz_general_error'];
    }

    // Thumbnail Creator
function make_thumbnails($updir, $img)
    {
    global $sourcedir;

    $thumbnail_width = 45;
    $thumbnail_height = 45;

    $arr_image_details = GetImageSize("$updir"."$img");
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image, $sourcedir . "/shop/Wizards_Gallery/small/" . "$img");
}
}

// Creates thumbnail and shows thumnail image in template - need $boardurl not $scripturl 
if(isset($_POST['Submit']) && !$errors) {
    make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";
$context['wizard_thumb_image'] = $boardurl . "/Sources/shop/Wizards_Gallery/small/" . $img_rname;
$context['wizard_uploaded_successfully'] = '<p>'.$feedback.'</p>File Uploaded Successfully!';
}

    // Proceed to Step 2
    $context['wizard_Step2'] = $scripturl . "?action=admin;area=wizard_add_item_step2";

} // End of Function wizard bracket

Kays

OK, do not place a function within a function. Remove the make_thumbnails() from where it is and add if after the wizard_add_item() function.

Just a note on this in that SMF does have the resizeImageFile() function in Subs-Graphics.php (not documented) which does essentially the same. As well, this function also checks for the existence of the GD libraries. Something which you should do also.


    $size=filesize($_FILES['image']['tmp_name']);


It is advisable not to query a file while it is still in the temp folder. If open_basedir is enabled on the server, then there will be an error returned rather than the file size. In this case, you can use the file size from the $_FILES array.


// check and make sure file size is not over limit if so do not upload and send error message
if ($size > MAX_SIZE*$max_file_size){
        $errors=1;
$context['wizard_error_size'] =  $txt['wiz_size_error'];
    }


I don't think the above will work since the value of MAX_SIZE is the same as $max_file_size. ???

As well if there is an error, it would be advisable to delete the uploaded file whether is be in the temp folder or the upload folder.


    $copied = copy($_FILES['image']['tmp_name'], $newname);


For security reasons, do not use copy(). The proper function to use is move_uploaded_file().

Quote from:  PHP manual
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system

Hard coded text in the following lines:

$feedback .= "thumbnail created $img_rname<br />";


$context['wizard_uploaded_successfully'] = '<p>'.$feedback.'</p>File Uploaded Successfully!';







If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

NanoSector

Quote from: The Wizard on December 16, 2012, 11:47:43 PM
Hello:

Thank you all for looking at my code and for all the wonderful suggestions. Below is the latest updated code with all your suggestions. If you see anything else I need to fix / change please let me know.

Thanks

Wiz

Note: Some of you have commented about my indentation. The simple fact is I don't know about the line ups as  I have never learned this step so I'm going with my own judgment and the fact I like straight lines. - wiz


if (!defined('SMF'))
die('Hacking attempt...');

Function wizard_add_item ()
{

global $sourcedir, $context, $boardurl, $scripturl, $txt;

loadTemplate('wizard_add_item_step1');

    // add to language file - just here for testing
    $txt['wiz_file_extension_error'] =  '<br />You used a Image File extension that is not a png.<br />Unable to proceed.';
    $txt['wiz_size_error'] = '<br />You have exceeded the size limit!<br />Copy unsuccessfull!';
    $txt['wiz_general_error'] = '<br />Unable to upload your image for some unknown reason<br />Please check image and server';
   
// path to directory where big uploaded image goes

        $upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

    $image=$_FILES['image']['name'];

    $newname = $upload_dir.$image;

    $img_rname = $image;

    // error checking system set to zero
    $errors=0;

// Maximum Size a image can be
        $max_file_size = 500;
   
    // get the file exention of the uploaded image
      $extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
       
    // check and make sure the file exntion is only "png" if not do not upload and send error message   
    if(isset($_POST['Submit']) && $extension != "png") {
    $errors=1;
$context['wizard_error_file_extension'] =  $txt['wiz_file_extension_error'];
} else {
    define ("MAX_SIZE","$max_file_size");
    $size=filesize($_FILES['image']['tmp_name']);
}

// check and make sure file size is not over limit if so do not upload and send error message 
if ($size > MAX_SIZE*$max_file_size){
        $errors=1;
$context['wizard_error_size'] =  $txt['wiz_size_error'];
    }

// If there are no errors the upload image to big file if not do not upload and send error message
// I did try move_uploaded_file() instead of copy() but move_uploaded_file() slowed down my server so I opted for copy()
if(isset($_POST['Submit']) && !$errors) {
   $copied = copy($_FILES['image']['tmp_name'], $newname);
    } else {
   $errors=1;
   $context['wizard_error_general'] =  $txt['wiz_general_error'];
    }

    // Thumbnail Creator
function make_thumbnails($updir, $img)
    {
    global $sourcedir;

    $thumbnail_width = 45;
    $thumbnail_height = 45;

    $arr_image_details = GetImageSize("$updir"."$img");
    $original_width = $arr_image_details[0];
    $original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image, $sourcedir . "/shop/Wizards_Gallery/small/" . "$img");
}
}

// Creates thumbnail and shows thumnail image in template - need $boardurl not $scripturl 
if(isset($_POST['Submit']) && !$errors) {
    make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";
$context['wizard_thumb_image'] = $boardurl . "/Sources/shop/Wizards_Gallery/small/" . $img_rname;
$context['wizard_uploaded_successfully'] = '<p>'.$feedback.'</p>File Uploaded Successfully!';
}

    // Proceed to Step 2
    $context['wizard_Step2'] = $scripturl . "?action=admin;area=wizard_add_item_step2";

} // End of Function wizard bracket

Try reading up on indentation here:
http://en.wikipedia.org/wiki/Indent_style

Allman style: http://en.wikipedia.org/wiki/Indent_style#Allman_style
is what SMF uses.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

The Wizard

Hello:

Every time I try and use the code below the script pitches a fit. So did I code it wrong? Because I don't think so.

Wiz



// check and make sure file size is not over limit if so do not upload and send error message 
if ($_FILES['image']['size'] > 500){
        $errors=1;
$context['wizard_error_size'] =  $txt['wiz_size_error'];
    }


below is the latest version minus the size limit code -



if (!defined('SMF'))
die('Hacking attempt...');

Function wizard_add_item ()
{

global $sourcedir, $context, $boardurl, $scripturl, $txt, $smcFunc, $modSettings;

loadTemplate('wizard_add_item_step1');

    // add to launage file
$txt['wiz_file_uploaded'] = '<br />File Uploaded Successfully!';

    // path to directory where big uploaded image goes
        $upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

    // path to directory where created thumbnail goes
    $thumbnail_dir = $sourcedir . "/shop/Wizards_Gallery/small/";

    $image=$_FILES['image']['name'];

    $newname = $upload_dir.$image;
   
    // With and Height of created thumbnail
$max_width = 45;
    $max_height = 45;

// error checking system set to zero
    $errors=0;

// Maximum Size a image can be
        $max_file_size = 500;
   
    // get the file exention of the uploaded image
      $extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
       
    // check and make sure the file exntion is only "png" if not do not upload and send error message   
    if(isset($_POST['Submit']) && $extension != "png") {
    $errors=1;
$context['wizard_error_file_extension'] =  $txt['wiz_file_extension_error'];
}

// check and make sure file size is not over limit if so do not upload and send error message 
//if ($_FILES['image']['size'] > 500){
        //$errors=1;
//$context['wizard_error_size'] =  $txt['wiz_size_error'];
   // }

// If there are no errors the upload image to big file if not do not upload and send error message
if(isset($_POST['Submit']) && !$errors) {
   move_uploaded_file($_FILES['image']['tmp_name'], $newname);
    } else {
   $errors=1;
   $context['wizard_error_general'] =  $txt['wiz_general_error'];
    }

// creates thumbnail and shows image of thumbnail in template
if(isset($_POST['Submit']) && !$errors) {
        require_once($sourcedir . '/Subs-Graphics.php');
    $thumbnail = resizeImageFile($newname, $thumbnail_dir . $image, $max_width, $max_height);
$context['wizard_thumb_image'] = $boardurl . "/Sources/shop/Wizards_Gallery/small/" . $image;
$context['wizard_uploaded_successfully'] = $txt['wiz_file_uploaded'];

// Proceed to Step 2
    $context['wizard_Step2'] = $scripturl . "?action=wizard_add_item_page2";
}
} // End of Function wizard bracket


The Wizard

Hello:

I have found a php formatter that does Allman style and I have few questions in setting it up -

Indent with: Spaces or Tabs?
Starting indentation: 0?
Indentation:4?

Yes or no Questions -

Remove all comments
Remove empty lines
Align assignments statements nicely
Put a comment with the condition after if, while, for, foreach, declare and catch statements
Remove lines with just a semicolon (;)
Make normal comments (//) from perl comments (#)
Make long opening tag (<?php) from short one (<?)
Space inside brackets - ( )
Space inside empty brackets - ( )
Space inside block brackets - [ ]
Space inside empty block brackets - [ ]


emanuele

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Hello:

I have found a php formatter that does Allman style and I have few questions in setting it up -
I would suggest you to format the code yourself while writing it instead of depend on automatic tools (it would start as a sort of exercise and end up in a (good) practice).
The SMF coding guidelines are here:
http://wiki.simplemachines.org/smf/Coding_Guidelines
and in particular you can read those regarding php here:
http://wiki.simplemachines.org/smf/Coding_Guidelines#PHP
there is not a lot to remember. ;)

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Indent with: Spaces or Tabs?
Tabs

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Starting indentation: 0?
I'd say 0, but take a to look at the result and see yourself what's best. ;)

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Indentation:4?
...if it is tab it shouldn't matter...I think.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Remove all comments
No?

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Remove empty lines
Not always...that's one of the problem with automated tools: they cannot distinguish when something could help readability or not.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Align assignments statements nicely
Maybe...

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Put a comment with the condition after if, while, for, foreach, declare and catch statements
???
Not sure what hat means...ahh...maybe I got it. If it is what I think it's up to you, I wouldn't use it.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Remove lines with just a semicolon (;)
yes...?

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Make normal comments (//) from perl comments (#)
No.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Make long opening tag (<?php) from short one (<?)
No!

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Space inside brackets - ( )
No.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Space inside empty brackets - ( )
No.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Space inside block brackets - [ ]
No.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Space inside empty block brackets - [ ]
No.

BTW:
Quote from: SA™ on December 15, 2012, 04:54:22 PM
line 106
should use  $sourcedir instead of $boardurl . "/Sources
Nope!
This is a design flaw of the mod he is using (not the one he is coding, but the one he relies on to create his script), that is placing images under Sources, so in order to display images into the HTML he has to use $boardurl . "/Sources
I know...


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

Hello:

Below is my nice shiny new code, but The size code DOES NOT WORK and I'm at a loss. I have done several tests and everything else works, but it will accept images over the size limit. Any ideas as to why?

Wiz


<?php

// =============================================================================
// Subs-Wizards_Add_Item.php                                
//
// Wizards Gallery: Gallery MOD for Simple Machines Forum Shop Mod                
// =============================================================================
//                                      
// Wizards Gallery by:         The Wizard ([email protected])  
// Version Number:             V 3.9d
// Copyright 2012 by:          The Wizard                                        
//
// Wizards Gallery was designed to be used with the Shop Mod for SMF
//
// SMF SHOP Software by:       DanSoft Australia (http://www.dansoftaustralia.net/)
// Copyright 2009 by:          vbgamer45 (http://www.smfhacks.com)                
// Copyright 2005-2007 by:     DanSoft Australia (http://www.dansoftaustralia.net/)
// Support, News, Updates at:  http://www.dansoftaustralia.net/                  
//                                                                                
// Forum software by:          Simple Machines (http://www.simplemachines.org)    
// Copyright 2006-2007 by:     Simple Machines LLC (http://www.simplemachines.org)
//           2001-2006 by:     Lewis Media (http://www.lewismedia.com)            
//
// Terms of the provided license:
// This program is free software; you may redistribute it and/or modify it.
// You are not allowed to sell this software, or package it in with software for sale.
//
// This program is distributed in the hope that it is, and will be useful, but    
// WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY  
// or FITNESS FOR A PARTICULAR PURPOSE.
//                  
// ==============================================================================

if (!defined('SMF'))
die('Hacking attempt...');

Function
wizard_add_item()
{
global $sourcedir, $context, $txt, $boardurl, $scripturl;

loadTemplate('wizard_add_item_step1');

// add to launage file
$txt['wiz_file_uploaded'] = '<br />File Uploaded Successfully!';

// path to directory where big uploaded image goes
$upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

// path to directory where created thumbnail goes
$thumbnail_dir = $sourcedir . "/shop/Wizards_Gallery/small/";

$image = $_FILES['image']['name'];

$newname = $upload_dir . $image;

// With and Height of created thumbnail
$max_width  = 45;
$max_height = 45;

// error checking system set to zero
$errors = 0;

// Maximum Size a image can be
$max_file_size = 500;

// get the file exention of the uploaded image
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);

// check and make sure the file exntion is only "png" if not do not upload and send error message  
if (isset($_POST['Submit']) && $extension != "png")
{
$errors                                 = 1;
$context['wizard_error_file_extension'] = $txt['wiz_file_extension_error'];
}

// check and make sure file size is not over limit if so do not upload and send error message  
if ($_FILES['image']['size'] > 500)
{
$errors                       = 1;
$context['wizard_error_size'] = $txt['wiz_size_error'];
}

// If there are no errors the upload image to big file if not do not upload and send error message
if (isset($_POST['Submit']) && !$errors)
{
move_uploaded_file($_FILES['image']['tmp_name'], $newname);
}
else
{
$errors                          = 1;
$context['wizard_error_general'] = $txt['wiz_general_error'];
}

// creates thumbnail and shows image of thumbnail in template
if (isset($_POST['Submit']) && !$errors)
{
require_once($sourcedir . '/Subs-Graphics.php');
$thumbnail                               = resizeImageFile($newname, $thumbnail_dir . $image, $max_width, $max_height);
$context['wizard_thumb_image']           = $boardurl . "/Sources/shop/Wizards_Gallery/small/" . $image;
$context['wizard_uploaded_successfully'] = $txt['wiz_file_uploaded'];

// Proceed to Step 2
$context['wizard_Step2'] = $scripturl . "?action=wizard_add_item_page2";
}
}
// End of Function wizard bracket

?>

The Wizard

Hello:

The following code below WORKS on checking if the image size is too big or not. I'm not sure exactly why it only works with 'tmp_name' you also have to use "define( )". It would be interesting to know why? I'm going with this code unless someone has a valid reason not for me to use it? If so please provide something to replace it that does work.

Wiz


// check and make sure file size is not over limit if so do not upload and send error message
// this code only works with 'tmp_name'
define ("MAX_SIZE","500");
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*500)
{
$errors                       = 1;
$context['wizard_error_size'] = $txt['wiz_size_error'];
}

NanoSector


// check and make sure file size is not over limit if so do not upload and send error message
// this code only works with 'tmp_name'
$max_size = 500;
$size=filesize($_FILES['image']['tmp_name']);
if ($size > ($max_size * 500))
{
$errors                       = 1;
$context['wizard_error_size'] = $txt['wiz_size_error'];
}


* Yoshi2889 puts brackets around (the (most (useless))) things and hopes they work.

Quote from: The Wizard on December 18, 2012, 07:42:51 AM
Make long opening tag (<?php) from short one (<?)
Definitely yes. Some servers go all mad when you have just <? in your file; their PHP install doesn't allow it whereas <?php works on ALL PHP installations (unless it's broken).
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

The Wizard

Quote* Yoshi2889 puts brackets around (the (most (useless))) things and hopes they work.

OK I have to ask is this meant as a joke? or what?

NanoSector

Quote from: The Wizard on December 18, 2012, 12:17:43 PM
Quote* Yoshi2889 puts brackets around (the (most (useless))) things and hopes they work.

OK I have to ask is this meant as a joke? or what?
The code is no joke, the comment below it is ;)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

The Wizard

ah ok so do you see any problem with the code?

Wiz

Advertisement: