Help - Search - Members - Calendar
Full Version: Shouldnt this be echoing something different?
HTMLHelp Forums > Programming > Server-side Scripting
allenph
I know I post a thousand threads sorry... so my issue is...

<?php
$uploaded_boolean = file_exists($_FILES["uploaded_avatar"]["…
$gif = '.gif';
$jpg = '.jpg';
$jpeg = '.jpeg';
$png = '.png';
$jpeg1 = 'jpeg';
$gif1 = 'gif';
$jpg1 = 'jpg';
$png1 = 'png';
$max_file_size = '1048576';
$new_avatar = $_FILES['uploaded_avatar']['name'];
$avatar_ext = end(explode('.', $new_avatar));
if (isset($_POST['change_submit1'])) {
if ($_SESSION['logged_in'] != '1') {
echo 'You need to be logged in!';
} elseif ($_POST['change_box1'] != 'CHANGE') {
echo 'You must fill in the "CHANGE" box!';
} elseif ($uploaded_boolean == False) {
echo 'No file selected!';
} elseif (($avatar_ext != $jpg1) && ($avatar_ext != $png1) && ($avatar_ext != $gif1) && ($avatar_ext != $jpeg1)) {
echo 'File format not supported!';
} elseif ($_FILES["uploaded_avatar"]["size"] > $max_file_size) {
echo 'File is too big!';
}
}
?>

It seems like it would work...but it outputs no file selected even though I want it to say File format not supported. They are in the correct order. am i checking for the file upload incorrectly or something? help please!
Brian Chandler
QUOTE
I know I post a thousand threads sorry...


That's not really a problem, but it helps if you can reduce the simple volume of code in your question.

There is *lots* that could be trimmed out anyway -- what is the point of having a *variable* $jpg whose value is always(?) 'jpg'? (Ans: none)

What you need hereabouts is an array that holds the valid extensions:

$validext = array('jpg', 'gif', 'png');

... or something like that. Then you check if the extension is valid using in_array() (which you look up in the manual for the details). This gets rid of all these sections where you repeat essentially the same code for a number of cases.

Actually you should read up on the gd library if you are having user upload "avatars" (which I presume are meant to be small images), because people will upload all sorts of bloated junk, and you should check they are valid image files and resize them appropriately. After all, there is nothing to stop a user renaming virus.exe as picture.jpg

Again, you have to find out why your program isn't working as you expect (of course it's "working", it just isn't quite the right program), but adding debugging echos. What _is_ the value of $avatar_ext ??
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.