-
Same Error?
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 Code:
<?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');
?>
-
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.
-
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.
-
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
-
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
-
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.
-
(omg, double post. can't edit above post!)
This code alone works:
PHP Code:
<?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
PHP Code:
$file_name = &$_FILES['file']['name'];
don't catch the right filename, because when I set it myself, it worked :}
-
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.
-
I'm such a fail for not reading your post before. Sorry for wasting your time :P
-
PHP Code:
<?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?