herghost Posted September 10, 2010 Share Posted September 10, 2010 Hi all, I have a simple upload form which is just to upload an image to a folder, this all works fine apart from one bit, once the image has been submitted then a box appears thanking the user for their upload and a close box link, then the original form also appears! I dont want this to appear once I file has been uploaded. I thought my below code would achieve that with an if statement but it is not working: <?php session_start(); $user_id = $_SESSION['user_id']; if (file_exists($user_id.'.'.$extension)) { define ("MAX_SIZE","100"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $image_name=$user_id.'.'.$extension; $newname="romimages/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; }}}} if(isset($_POST['Submit']) && !$errors) { ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form class="dark" action="" method="post" > <ol> <li> <fieldset> <legend>Rom Logo Uploaded!</legend> <p> </p> <ol> <li> <label for="">Your image has been uploaded, you can now close this window</label> </li> </ol> </fieldset> </li> </ol> <br> <input type="submit" value="Close" name="submit" onClick="return parent.parent.GB_hide()" > </p> </form> <?php } } else { ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form method="post" enctype="multipart/form-data" action="" class="dark"> <ol> <li> <input type="file" name="image" width="45px"> </li><br> <br> <li> <input name="Submit" type="submit" value="Upload image"> </form> <?php } ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/ Share on other sites More sharing options...
KevinM1 Posted September 10, 2010 Share Posted September 10, 2010 Where does $extension come from? You use it on line 4 before defining it. Also, be sure to check your braces. I don't know if this is the way you normally indent your code, or if pasting it here messed up the structure, but it's hard to read, especially when it comes to lining up braces. Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1109752 Share on other sites More sharing options...
herghost Posted September 10, 2010 Author Share Posted September 10, 2010 Thanks, I wonder why I didnt get a parse error on that before :/ Ah yes, the formatting, its a new type I call dave formatting, a total mess, but I know where everything is! I have cleaned it up, can you think of another way of what I am trying to prove? Obviously the if file exists work once the file exists, if it doesnt then it is going to produce a parse error. <?php session_start(); $user_id = $_SESSION['user_id']; define ("MAX_SIZE","100"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; if(isset($_POST['Submit'])) { $image=$_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } $image_name=$user_id.'.'.$extension; $newname="romimages/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } } } } if(isset($_POST['Submit']) && !$errors) { ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form class="dark" action="" method="post" > <ol> <li> <fieldset> <legend>Rom Logo Uploaded!</legend> <p> </p> <ol> <li> <label for="">Your image has been uploaded, you can now close this window</label> </li> </ol> </fieldset> </li> </ol> <br> <input type="submit" value="Close" name="submit" onClick="return parent.parent.GB_hide()" > </p> </form> <?php } ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form method="post" enctype="multipart/form-data" action="" class="dark"> <li> <input type="file" name="image" width="45px"> </li><br> <br> <li> <input name="Submit" type="submit" value="Upload image"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1109753 Share on other sites More sharing options...
herghost Posted September 10, 2010 Author Share Posted September 10, 2010 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1109789 Share on other sites More sharing options...
KevinM1 Posted September 11, 2010 Share Posted September 11, 2010 Try something along these lines (note: I have not tested this, so no guarantee it will work right off): <?php define ("MAX_SIZE", "100"); function getExtension($str) { $i = strrpos($str, "."); if(!$i) { return ""; } else { $l = strlen($str) - $i; $ext = substr($str, $i + 1, $l); return $ext; } } session_start(); $user_id = $_SESSION['user_id']; $errors = false; if(isset($_POST['Submit'])) { $image = stripslashes($_FILES['image']['name']); if($image) { $size = filesize($_FILES['image']['tmp_name']); $extension = strtolower(getExtension($image)); if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors = true; } elseif($size > (MAX_SIZE * 1024)) { echo '<h1>You have exceeded the size limit!</h1>'; $errors = true; } else { $image_name = $user_id . '.' . $extension; $newname = "romimages/" . $image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if(!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors = true; } } } if(!$errors) { ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form class="dark" action="" method="post"> <ol> <li> <fieldset> <legend>Rom Logo Uploaded!</legend> <p> </p> <ol> <li> <label for="">Your image has been uploaded, you can now close this window</label> </li> </ol> </fieldset> </li> </ol> <br> <input type="submit" value="Close" name="submit" onClick="return parent.parent.GB_hide()" > </form> <?php } } ?> <link href="../Styles/form_dark.css" rel="stylesheet" type="text/css" /> <form method="post" enctype="multipart/form-data" action="" class="dark"> <li> <input type="file" name="image" width="45px"> </li> <br> <br> <li> <input name="Submit" type="submit" value="Upload image"> </form> In general, you really just need to tidy your code. There's an overall feeling of "I'm gonna throw this code at the wall and see what sticks" here. Go through it step-by-step and don't make it more complicated than it needs to be. Also, I strongly suggest writing semantically meaningful markup. Your error condition HTML can be made much simpler and much smaller. You shouldn't create a form with no action just to create a button. HTML has a generic <button> element that's used for this exact problem. Similarly, lists shouldn't be used just because. A list denotes, well, a list of things. They're often used with navigation because navigation is often just a list of links. Just use the right tools for the job. A couple of divs with proper styling is a much more efficient way of doing what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1109831 Share on other sites More sharing options...
herghost Posted September 11, 2010 Author Share Posted September 11, 2010 Many Thanks, I have tidied up as suggested (their is a div in the works!) but I seemed to have changed something drastic that I cannot work out! It now just shows the rom image uplaod succesful page instead of the form when the link is clicked on ? <link href="../style.css" rel="stylesheet" type="text/css" /> <?php session_start(); $user_id = $_SESSION['user_id']; define ("MAX_SIZE", "100"); function getExtension($str) { $i = strrpos($str, "."); if(!$i) { return ""; } else { $l = strlen($str) - $i; $ext = substr($str, $i + 1, $l); return $ext; } } $errors = false; if(isset($_POST['Submit'])) { $image = stripslashes($_FILES['image']['name']); if($image) { $size = filesize($_FILES['image']['tmp_name']); $extension = strtolower(getExtension($image)); if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo '<h1>Unknown extension!</h1>'; $errors = true; } elseif($size > (MAX_SIZE * 1024)) { echo '<h1>You have exceeded the size limit!</h1>'; $errors = true; } else { $image_name = $user_id . '.' . $extension; $newname = "romimages/" . $image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if(!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors = true; } } } $_SESSION['extension'] = $extension; if(!$errors) {?> <form method="post" enctype="multipart/form-data" action="" class="dark"> <input type="file" name="image" width="45px"><br /> <input name="Submit" type="submit" value="Upload image"> </form> <?php }} ?> <link href="../style.css" rel="stylesheet" type="text/css" /> Rom Logo Uploaded!<br /> <img src="romimages/<?php echo $user_id;?>.<?php echo $_SESSION['extension'];?>"/><br /> <button type="button" onclick="return parent.parent.GB_hide()">Close</button> Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1110016 Share on other sites More sharing options...
BizLab Posted September 11, 2010 Share Posted September 11, 2010 You could just use a standard $display_form = true; if($_POST['image']){ $display_form = false; // process image } // image processing code if($display_form){ // form code } that always works well for me and it's nice and clean code Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1110020 Share on other sites More sharing options...
herghost Posted September 11, 2010 Author Share Posted September 11, 2010 Thanks, but whats the explanantion for the post ['image'] code? I dont have a variable image? Am I missing something again??!! Quote Link to comment https://forums.phpfreaks.com/topic/213088-showing-when-shouldnt/#findComment-1110024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.