Welcome to WebmasterWorld Guest from 54.167.46.29

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Regular Expressions

Code Not Working - Gives Errors

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

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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.

Regards

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

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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....
$thumb_path.'/'.$thumb_img


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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


That's a bit of a puzzle. Try
var_dump($move_file);

And see what you get.
2:57 pm on Nov 27, 2013 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 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)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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)

Junior Member

5+ Year Member

joined:Nov 10, 2006
posts: 140
votes: 0


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