homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Regular Expressions
Code Not Working - Gives Errors

 8:50 pm on Nov 21, 2013 (gmt 0)

Hello there webmaster world, hope we all are keeping well and geared up for the festive season that is just round the corner.

I have a problem with REGEX with a form that I am using, wherein an image too is uploaded.

The field for the image upload is thumbs. What I want to achieve is if the file extension is not gif or jpg or png, then an error should be shown as below.

So here is my code.

$thumb_type = $_POST['thumbs']['type'];

if(!preg_match('/^.(gif|jpg|png)$/', $thumb_type)) {
echo 'Error! - Invalid file type detected';

What is happening is even when I try to upload a jpg file, it is giving me the error message, which indicates that something is wrong with my above code.

Could the experts on REGEX please review my coding and advise where I have gone wrong or what changes should I bring in the code to get this working.

Your assistance, as always, is highly appreciated.




brotherhood of LAN

 8:59 pm on Nov 21, 2013 (gmt 0)

Hello Melwyn,

Have you tried just "print_r($_POST);exit(0);" to see if the variable exists? If it's an image upload the variables may be in $_FILES rather than $_POST

You might also want to edit your regex to /^\.(gif|jpe?g|png)$/i

- "\." rather than "." to match a literal period rather than "." on its own would match any character.

- jpe?g as it can be either jpg or jpeg

- the "i" modifier to make it case insensitive, iirc PHP doesn't normalise the type to any particular case.


 9:14 pm on Nov 21, 2013 (gmt 0)

Opsss. Sorry, it is actually, $_FILES, habitually typed it as $_POST in here.

Thanks BOL, I tried your code, but I am still getting the same error.


 9:23 pm on Nov 21, 2013 (gmt 0)

$thumb_type = $_POST['thumbs']['type'];

The 'type' element (if present at all) holds the mime-type, not the filename/extension. eg. "image/gif"


 9:32 pm on Nov 21, 2013 (gmt 0)

Okay, got it sorted as below:

$thumb = $_FILES['thumbs']['name'];
$ext = strrchr($thumb,'.');

if(!preg_match( '/^\.(gif|jp?g|png)$/i', $ext)) {
$error .= 'Error! - Invalid file type detected

Thanks penders for that little piece of invaluable knowledge that you provided, which led me to use the above improvise.


 10:34 pm on Nov 21, 2013 (gmt 0)

Okay, moving on from there, I have hit another road block and this, I believe, is something very stupid oversight from my end, but for my life, I am unable to figure it out.
My form/script sits in a folder called forms under the root directory.

The file fails to get uploaded. Here is the code.

$thumb_img = $_FILES['thumbs']['name'];
$temp_name = $_FILES['thumbs']['temp_name'];
$thumb_path = '../thumbnails';
$move_file = move_uploaded_file($temp_name,'$thumb_path/$thumb_img');

if ($move_file = true){ echo 'Success';} else {echo 'Fail';}

The message I am getting is Success, but the file does not seem to have been uploaded and I am baffled!

Is there some thing that is wrong with the path? I have checked the permissions for the directory, and they seem to be fine.

Where have I gone wrong with this one?


 10:57 pm on Nov 21, 2013 (gmt 0)

$thumb = $_FILES['thumbs']['name'];

I would have thought you are still better off comparing against the 'type' (mime-type), but this may depend on your user base. (Not every OS need/use file extensions to determine type.)

$thumb_path = '../thumbnails';  
$move_file = move_uploaded_file($temp_name,'$thumb_path/$thumb_img');

Single quoted strings don't expand variable names, you'll need to use double quotes, or use string concatenation....

With single quotes you'll have written to the file '$thumb_path/$thumb_img' (literally) - so if that was successfull, you'll have a strangely named file on your filesystem!

But I wouldn't use relative paths. Use an absolute path...
$thumb_path = $_SERVER['DOCUMENT_ROOT'].'/thumbnails';

 11:13 pm on Nov 21, 2013 (gmt 0)

Thanks penders.

Despite making the following changes as suggested, not sure where the hell the file is going or what is happening.

$thumb_path = $_SERVER['DOCUMENT_ROOT'] .'/thumbnails';
$move_file = move_uploaded_file($temp_name, $thumb_path .'/' .$thumb_img);


 11:29 pm on Nov 21, 2013 (gmt 0)

Do you have the appropriate permissions set on your "thumbnails" directory?

if ($move_file = true){ echo 'Success';} else {echo 'Fail';}

I've just noticed that you are using an assignment operator (=) as opposed to a comparison operator for equality (==), so this will always echo 'Success' no matter what!

if ($move_file) { 
echo 'Success';
} else {
echo 'Fail';

Or (using the ternary operator)...
echo $move_file ? 'Success' : 'Fail';


 12:24 am on Nov 22, 2013 (gmt 0)

Just to add... enable full error_reporting at the start of your script, if you haven't already...

error_reporting(-1); // All errors reported
ini_set('display_errors','1'); // Or check your error log


 8:46 am on Nov 22, 2013 (gmt 0)

Many many thanks @penders.

After a review of the error report, it was flagged that there was an error as below:

$temp_name = $_FILES['thumbs']['temp_name'];

It should have been $temp_name = $_FILES['thumbs']['tmp_name'];

It is now working; I am relieved.


 10:57 am on Nov 22, 2013 (gmt 0)

One final issue to be resolved.

$move_file = move_uploaded_file($resized_img, $thumb_path .'/' .$rand_name . '.' .$file_ext);
echo $move_file ? 'Success' : 'Fail';

The file gets uploaded successfully to my thumbnails directory, however, the message I get is Fail. Why is this happening? No other errors are detected.

Any ideas how I can get the confirmation of successful upload?


 12:39 pm on Nov 22, 2013 (gmt 0)

That's a bit of a puzzle. Try
And see what you get.


 2:57 pm on Nov 27, 2013 (gmt 0)

@penders, this is now working like a charm. Thank you for your input in resolving my issues with REGEX.


 4:06 pm on Nov 27, 2013 (gmt 0)

Glad you got it resolved.

I'm curious what the problem was with move_uploaded_file() appearing to return False on success?


 5:32 pm on Nov 27, 2013 (gmt 0)

Lol, the problem was I had put the code the other way round, silly of me really.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved