This topic is locked

If wrong file is uploaded

7/14/2008 12:20:11 AM
PHPRunner General questions
I
indigo author

Hi,
I need a BeforeAdd/BeforeUpdate event, so when a non-image file is being uploaded, user gets an error.
I have five image fields ,

Photo1

Photo2

Photo3

Photo4

Photo5
So i want to check if the fields have any input and if yes, then the extension allowed is only GIF, JPG, BMP, JPEG, PNG. Else give an ECHO error.
Please help me on this.

J
Jane 7/14/2008

Hi,
check $_FILES["value_ImageFieldName"]["name"] variable in the Before record added/updated events for this purpose.

I
indigo author 7/14/2008

Could you please elaborate. I tried doing and could not do it properly...
How do I achieve checking of filetypes?

K
kdeloach 7/14/2008

Could you please elaborate. I tried doing and could not do it properly...

How do I achieve checking of filetypes?


$file = "test.jpg";
// yields jpg

$ext = basename( $n=substr($file, strpos($file, '.')+1) );
$images = array( 'jpg', 'jpeg', 'gif', 'png' );
if( in_array($ext, $images) ){

echo "Files is an image";

} else {

echo "Error - file is not an image";

}

I
indigo author 7/16/2008

Hi,
I tried doing the above.
However, the image field is set to create thumbnails and that is executed first. Since that code gives a PHP error "imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format", the code for checking of the file extension becomes redundant.
Is it possible to give a pop-up for non-image content before the thumbnail creation starts so the user can correct it?

J
Jane 7/16/2008

Hi,
thank you for pointing me to this bug.

We'll fix it in the next update.
To make your pages working open C:/Program Files/PHPRunner4.2/source/include/commonfunctions.php file, locate CreateThumbnail function, find this line:

$img = imagecreatefromstring($value);



and replace it with this one:

$img = @imagecreatefromstring($value);

I
indigo author 7/16/2008

Hi Jane,

Thanks for help.

The above change did not make any difference.
Currently this is my BeforeUpdate event...

$file = $_FILES["value_Photo2"]["name"];

$ext = basename( $n=substr($file, strpos($file, '.')+1) );

$images = array( 'jpg', 'jpeg', 'gif', 'png' );
if( in_array($ext, $images) ){

echo "Files is an image";

return true;

} else {

echo "Error - file is not an image";

return false;

}


And I have changed the commonfunctions code.
Pls help me if I am going wrong somewhere...

J
Jane 7/17/2008

It's difficult to tell you what's happening without seeing actual files.
Please publish your project on Demo Account and send to support@xlinesoft.com a URL to your pages along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.

M
mustafa 11/7/2008

Could you please elaborate. I tried doing and could not do it properly...

How do I achieve checking of filetypes?



It's actually easy.
First of all, there is a logical error in ( _edit.php ) file created.

These two lines from mine ()
$ext = CheckImageExtension($_FILES["[color=#999999]value_logo"]["name"]);

$thumb = CreateThumbnail($value,80,$ext);
Although function name starts with "check", returning value "false" is ignored.
Let me explain what to do:
1- in the commonfunctions.php ()

function CreateThumbnail($value, $size, $ext)

{

[color=#990000]if($ext==".GIF" || $ext==".JPG" || $ext=="JPEG" || $ext==".PNG" || $ext==".BMP") {

if(!function_exists("imagecreatefromstring"))

return $value;

$img = imagecreatefromstring($value);

.

.

.
} [color=#666666]// function ends


2- in the Before record added/updated events ()

$ext = CheckImageExtension($_FILES["[color=#666666]value_logo"]["name"]);

if ($ext) {

return true;

} else {

$message = "Please select an image file as your LOGO!";

return false;

}

[i]// return true if you like to proceed with editing this record

// return false otherwise



Best Regards,

Mustafa

M
mustafa 11/8/2008

2- in the Before record added/updated events (*)




Some modification is needed for allowing blank field to proceed.

$_FILES["value_logo"]["name"] = trim($_FILES["value_logo"]["name"]);

$ext = CheckImageExtension($_FILES["value_logo"]["name"]);
if (!($_FILES["value_logo"]["name"])){

return true;

} else {

if ($ext) {

return true;

} else {

$message = "Please select an image file as your LOGO!";

return false;

}

}



There is a bug:

If you put only some spaces (or spaces before/after the file name) in this field, an error occurs.

Field value must be trimmed before proceed to upload.
Best Regards,

Mustafa