close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 or 2018 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

File Upload Validation Query

Thread began 5/29/2015 1:53 pm by Paul | Last modified 6/03/2015 4:33 pm by Ray Borduin | 1189 views | 8 replies |

PaulBeta Tester

File Upload Validation Query

Using DB to create an upload function.

I've created an image width (max pixel value 940) validation, however, if it is found to exceed this value during upload, the file continues to save to the server - it doesn't fail and prevent save.

<
if (isset($_POST["nhSendUpload"]) || isset($_POST["nhSendUpload_x"])) {
$WAFV_Redirect = "";
$_SESSION['WAVT_nhAddPhoto1_202_Errors'] = "";
if ($WAFV_Redirect == "") {
$WAFV_Redirect = $_SERVER["PHP_SELF"];
}
$WAFV_Errors = "";
$WAFV_Errors .= WAValidateNM(((isset($_POST["nhImageLoadSize"]))?$_POST["nhImageLoadSize"]:"") . "",1,50000,"",",.",true,2);
$WAFV_Errors .= WAValidateNM(((isset($_POST["nhImageLoadWidth"]))?$_POST["nhImageLoadWidth"]:"") . "",1,940,"",",.",true,1);

if ($WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"nhAddPhoto1_202");
}
}
>

Any ideas why this is so?

Sign in to reply to this post

Ray BorduinWebAssist

Where are you getting the bindings for the size and width? They appear wrong. Can I see a copy of the page?

Sign in to reply to this post

PaulBeta Tester

They are from my upload form.

Page attached.

Attached Files
2015-05-29 Server-validation-issue-2.txt
Sign in to reply to this post

Ray BorduinWebAssist

Two things... first the validation appears below the database insert code. In order for it to prevent the insert you would have to put the validation below the file upload code and above the insert code.

Second the validated values appear wrong. I think it should be:

php:
<?php

if (isset($_POST["nhSendUpload"]) || isset($_POST["nhSendUpload_x"])) {
$WAFV_Redirect "";
$_SESSION['WAVT_nhAddPhoto1_202_Errors'] = "";
if (
$WAFV_Redirect == "") {
$WAFV_Redirect $_SERVER["PHP_SELF"];
}
$WAFV_Errors "";
$WAFV_Errors .= WAValidateNM(((isset($_FILES["nhImageUpload"]))?$_FILES["nhImageUpload"]["size"]:"") . "",1,50000,"",",.",true,2);
$WAFV_Errors .= WAValidateNM(((isset($_FILES["nhImageUpload"]))?$_FILES["nhImageUpload"]["width"]:"")  . "",1,940,"",",.",true,1);

if (
$WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"nhAddPhoto1_202");
}
}
?>
Sign in to reply to this post

PaulBeta Tester

I get the role of the validation being placed before the insert.

However despite the two changes, images are failing validation and saving to my server.
They are not inserting into my database though, so its kinda getting there.

I'm wondering if its because the image file size is being reduced to below the validation limit.
100Kb + files are for some reason reducing to less than 40Kb after uploading still !

Sign in to reply to this post

Ray BorduinWebAssist

You would have to move the validation above the upload code too if you wanted it to stop the file upload from taking place.

Sign in to reply to this post

PaulBeta Tester

Ok so taking a verbose approach to my validation;

If the file size is between 1 - 50,000 Kb, upload to server
AND
If the image width is between 1 - 940 pixels, insert a record

If both these conditions are true during validation then the file should be uploaded and a record inserted.

Based on this, I have created the following WA Validation;

if (isset($_POST["nhSendUpload"]) || isset($_POST["nhSendUpload_x"])) {
$WAFV_Redirect = "";
$_SESSION['WAVT_nhAddPhoto1_202_Errors'] = "";
if ($WAFV_Redirect == "") {
$WAFV_Redirect = $_SERVER["PHP_SELF"];
}
$WAFV_Errors = "";
$WAFV_Errors .= WAValidateNM(((isset($_FILES["nhImageUpload"]))?$_FILES["nhImageUpload"]["size"]:"") . "",1,50000,"",",.",true,2);
$WAFV_Errors .= WAValidateNM(((isset($_FILES["nhImageUpload"]))?$_FILES["nhImageUpload"]["width"]:"") . "",1,940,"",",.",true,1);

if ($WAFV_Errors != "") {
PostResult($WAFV_Redirect,$WAFV_Errors,"nhAddPhoto1_202");
}
}

This code sits above the WA insert and upload server behaviors.

I am uploading a .jpg file 40Kb in size and 620 pixels in width, both meet my validation criteria and should not fail.
I am creating a thumbnail at the same time, but it is not being validated.

For some reason the upload is failing on one or both validations

If i remove the validation behavior, the file uploads perfectly, so the issue lays with how the validation is constructed.

Have i understood this correctly?

Sign in to reply to this post

Ray BorduinWebAssist

The issue is that the width won't be set automatically. The webassist upload sets it, but if you put the validation after that it will have already uploaded the file.

The solution is to set the width manually using the getimagesize() function.

If you add this code above the validation:

php:
<?php

if (isset($_FILES)) {
  
$image_info getimagesize($_FILES["nhImageUpload"]["tmp_name"]);
  
$_FILES["nhImageUpload"]["width"] = $image_info[0];
}
?>



That should add the width attribute to the files array and allow you to validate it.

Sign in to reply to this post

PaulBeta Tester

I think i get that!

Anyways, your solution and advice has worked. Thanks.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...