PDA

View Full Version : Same Error?



Shibby-Shabs
22-07-2011, 04:20 AM
My code below is checking the file but for some reason the file extension keeps being report wrong, not in the array of allowed extensions so I continuously see the error "Only JPEG, JPG, GIF and PNG files are allowed."


<?php
if (isset($_SESSION['login'])) {
backend_links();
$button = &$_POST['upload_button'];
$name = &$_POST['name'];

if (isset($button)) {

if (empty($name))
echo "<script type='text/javascript'>
$(function() {upload_error('All fields required')});
</script>";
else {
$file_name = &$_FILES['file']['name'];
$file_temp = &$_FILES['file']['tmp_name'];
$file_size = &$_FILES['file']['size'];
$file_explode = explode('.', $file_name);
$file_ext = strtolower(end($file_explode));
$allowed = array('jpeg', 'jpg', 'gif', 'png');

if (!in_array($file_ext, $allowed)) {
echo "<script type='text/javascript'>
$(function() {upload_error('Only JPEG, JPG, GIF and PNG files are allowed.')});
</script>";
}

elseif
($file_size > 2) {
echo "<script type='text/javascript'>
$(function {upload_error('File must be 2MB or Smaller!')});
</script>";
}
else {
echo "mamama";
}

}
}
?>
<br /><center><form action='home' method='post' encrypt='multipart/form-data'>
Image Name<br /><input type='text' name='name' autocomplete='off'> <br />
File<div id='file_upload'><span id='path'></span><input id='file' type='file' name='file'></div>
<input type='submit' value='Upload' name='upload_button'>
</form><center>
<?php

}
else header('Location: login');
?>

Pegle
22-07-2011, 08:34 AM
Maybe try adding the fullstop before the file extension, i'm no expert but it just seems to me like that could be the problem. If it's not, sorry I can't help you.

Shibby-Shabs
22-07-2011, 10:04 AM
That wouldn't work because the full stop is used to break say "image.png" into "image" and "png" and then end(); gets "png" as I understand, then I'm comparing that with the allowed extensions.

hamheyelliot
22-07-2011, 11:48 AM
I know absolutely nothing about PHP, so I'm basically just reading this in a sort of linear-ish fashion.

You establish the allowed extensions here: $allowed = array('jpeg', 'jpg', 'gif', 'png');

However the code directly below it seems to say: "If the file extension falls into the category 'allowed', display the message "Only such and such are allowed" and rejects the file.

I'm assuming the else { echo "mamama"; } is the successful outcome, so perhaps you should switch this with the one I mentioned before? Or replace the allowed file types with disallowed file types?

I've got no clue how to express it in PHP so I hope at least some of it helps :P

Shibby-Shabs
22-07-2011, 01:03 PM
You're sort of right there but the "!" represents "NOT" so "if the extension doesn't fall into the category 'allowed' " then echo my error otherwise it would echo "mamama".
Thank you for trying but I was told on PHPAcademy that the error was within my HTML form, encrypt is meant to be encrype

Homosexual
22-07-2011, 03:52 PM
Is the extension of the file uppercase or lowercase? in_array is case-sensitive :) < Scrap that, I see a strtolower(); :]

I'm having a look, if I DP sorry; I can't edit haha.

Homosexual
22-07-2011, 04:20 PM
(omg, double post. can't edit above post!)

This code alone works:


<?php
$file_name = "potato.PNG";
$file_explode = explode('.', $file_name);
$file_ext = strtolower(end($file_explode));
$allowed = array('jpeg', 'jpg', 'gif', 'png');

print_r($file_explode);
print_r($file_ext);
print_r($allowed);

if (!in_array($file_ext, $allowed)) {
echo 'File extension not accepted.';
} else {
echo 'File extension right. Carry on.';
}

?>


So all I can suggest is that
$file_name = &$_FILES['file']['name']; don't catch the right filename, because when I set it myself, it worked :}

Shibby-Shabs
22-07-2011, 04:24 PM
That would work because you have a file name - the error in my code was that it couldn't get the file name because I messed up the form.

Homosexual
22-07-2011, 09:13 PM
I'm such a fail for not reading your post before. Sorry for wasting your time :P

0x00
23-07-2011, 02:19 AM
<?php
if (isset($button)) {
$file_name = &$_FILES['file']['name'];
$file_explode = explode('.', $file_name);
$file_ext = strtolower(end($file_explode));
var_dump($file_ext);
echo '<br/>';
var_dump($file_name);
echo '<br/>';
var_dump($file_explode);
}


What's it returning?

Shibby-Shabs
23-07-2011, 05:48 AM
<?php
if (isset($button)) {
$file_name = &$_FILES['file']['name'];
$file_explode = explode('.', $file_name);
$file_ext = strtolower(end($file_explode));
var_dump($file_ext);
echo '<br/>';
var_dump($file_name);
echo '<br/>';
var_dump($file_explode);
}


What's it returning?

Don't understand what you mean, at that point I hadn't finished the script so I was waiting for that to work so I could continue.


I'm such a fail for not reading your post before. Sorry for wasting your time :P

Nah man thanks for trying anyway :)

0x00
23-07-2011, 08:09 AM
Don't understand what you mean, at that point I hadn't finished the script so I was waiting for that to work so I could continue.



Nah man thanks for trying anyway :)

When you submit the form, what's it showing with that code?

Shibby-Shabs
24-07-2011, 02:54 AM
Thanks for helping, I won't bother trying because the problem is solved and it was within the html form.

Want to hide these adverts? Register an account for free!