yujikaido Posted March 5, 2010 Share Posted March 5, 2010 I am working on my code trying to upload an image inside one of my functions. Although the upload is successful to my selected image directory I get sent back to the login page. I am thinking something isn't working right somewhere but I can't find it. the function is " picupload()" I have been working on it for days and made only a little progress. I am newbie in php and any help would be appreciated. Thanks. <?php function debug($msg) { echo("<p>*** ". $msg); } # Load incoming parameter value or return a null string function getparam($param) { if(isset($_REQUEST[$param])) { return($_REQUEST[$param]); } else { return(''); } } function loginform($u) { debug("Login Form"); echo <<<messageend <form action='loginphoto.php' method='post'> <font size="+2"> <table width=25% align=center bgcolor=#FFFFCC border=3> <form name="form" method="post" <td align=center bgcolor=#FABCCC</td <table align=center> <tr> <th>Name:</th> <td><input type='text' name='user' value='$u'></td> </tr> <tr> <th>Password:</th> <td><input type='password' name='passwd'></td> </tr> <tr> <td colspan=2 align='right'> <input type='submit' value='login'> </td> </tr> </table> </form> messageend; } function logout(&$user,&$passwd) { debug("Logout"); echo "<p align=center>[$user], you have been logged out"; echo "<p align=center><input type=submit value='Good-bye'>"; $user = ''; $passwd = ''; } function picslide() { debug("picture slide show"); echo "<p align=center>Not supported at this time"; echo "<p align=center><input type='submit' value='Menu'>"; } function picoption() { debug("picture options"); echo "<p align=center>Not supported at this time"; echo "<p align=center><input type='submit' value='Menu'>"; } function picupload() { debug("file upload"); echo("<p>Upload picture form"); echo "<form enctype='multipart/form-data' method='post' action='loginphoto.php'>"; echo "<p>Image: "; echo "<input name='file' type='file'>"; echo "<input type='submit' name='state' value='Upload'>"; echo "</form>"; } if ($_REQUEST['state'] == 'Upload') { echo("<p>Incoming file processing"); $target_path = "images/"; $target_path = $target_path . basename( $_FILES['file']['name']); echo "<p>Number of files: " . count($_FILES); echo "<p>Incoming File name: "; echo ($_FILES['file']['name']); echo "<p>Attempting..."; echo "<p>Copy from: " . $_FILES['file']['tmp_name']; echo "<p>Copy to: " . $target_path; if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "<p>File [". $_FILES['file']['name'] . "] has been uploaded"; } else { echo "<p>There was an error uploading the file to" . $target_path . " Please try again"; } echo "<form method='post' action='loginphoto.php'>"; echo "<input type='submit' name='state' value='Try again'>"; echo "</form>"; } function menu() { debug("Send menu"); echo <<<menuend <font size="+2"> <table width=25% align=center bgcolor=#FFFFCC border=3> <form name="form" method="post" <td align=center bgcolor=#FABCCC</td <table align='center'> <tr> <td colspan=2>Choose one:</td> </tr> <tr> <td><input type='radio' name='state' value='3'></td> <td>Slide Show</td> </tr> <tr> <td><input type='radio' name='state' value='2'></td> <td>Picture listing and options</td> </tr> <tr> <td><input type='radio' name='state' value='4'></td> <td>Picture Upload</td> </tr> <tr> <td><input type='radio' name='state' value='1'></td> <td>Logout </tr> <tr> <td colspan=2 align=center><input type=submit value="Go"> </table> menuend; } echo <<<headerend <html> <head> <title>Login Photo </title> </head> <body bgcolor="#F57A00"> <h1 style='font-family:sans-serif; color:brown;' align='center'>Login Photo Project</h1> headerend; $user = getparam('user'); $passwd = getparam('passwd'); $logins = array( 'luke' => 'cook', 'yuji' => 'kaido', 'guest' => 'pass', 'nathan' => 'lunsford' ); $state = getparam('state'); if (($logins[$user] == $passwd) && # IF AUTHENTICATED USER ($user != '') && ($passwd != '')) { # CHECK FOR DESIRED OPERATION switch($state) { case 1: # LOGOUT logout($user,$passwd); break; case 2: #Picture Options picoption(); break; case 3: # Picture slide Show picslide(); break; case 4: # Picture Upload picupload(); break; default: # SEND MENU menu(); } # SEND OUT THE AUTHENTICATION DATA echo "<input type='hidden' name='user' value='$user'>\n"; echo "<input type='hidden' name='passwd' value='$passwd'>\n"; } else { # SEND A LOG IN FORM if (($user != '') || ($passwd != '')) { echo "<p align=center style='color:red;'>Invalid login</p>"; } loginform($user); echo $logins[$user]; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/194293-php-code-help-please/ Share on other sites More sharing options...
kavisiegel Posted March 5, 2010 Share Posted March 5, 2010 First, your image handling logic isn't included in the picupload() function, I don't know if it was intended to. This, however, will cause anything on that page to look for an upload and handle it, even if the picupload function isn't called. Try to echo the $state variable, and see what state it returns after an upload. If it's number 4, then you're on the right track. You can also try to put quotes around the numbers in your case structure, right now it's comparing an int to a string. Link to comment https://forums.phpfreaks.com/topic/194293-php-code-help-please/#findComment-1022138 Share on other sites More sharing options...
yujikaido Posted March 5, 2010 Author Share Posted March 5, 2010 what do you mean by image handling? it uploads it to my designated folder. also i put the state variable in my case 4 function and when I select upload I see a number 4 . Sorry I am really lost. Link to comment https://forums.phpfreaks.com/topic/194293-php-code-help-please/#findComment-1022142 Share on other sites More sharing options...
kavisiegel Posted March 5, 2010 Share Posted March 5, 2010 From lines 90 to 121, you're dealing with the image upload. ("image handling") The function picupload() ends at line 88, which means it doesn't include the upload logic. This means that the code is executed without picupload() being executed, which seems to be your problem. do a var_dump on $state, it's probably not an int, so in your switch structure, put quotes around the numbers to make them into text. That should make sure it gets routed to the right case. Link to comment https://forums.phpfreaks.com/topic/194293-php-code-help-please/#findComment-1022148 Share on other sites More sharing options...
yujikaido Posted March 6, 2010 Author Share Posted March 6, 2010 i don't understand what you mean by upload logic. the upload function works if i put it in separate php file but when i put it in my function i get it to work but it go to the login page under the status of the upload. do you mean i am missing some html code before line 88 or some php code? This is getting frustrating but thanks for the replies. i had so much trouble learning C++ programming and gave up on it after a semester and this just seems the same. Link to comment https://forums.phpfreaks.com/topic/194293-php-code-help-please/#findComment-1022167 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.