Jump to content

php program to login,upload images and play slide show help needed


yujikaido

Recommended Posts

I am working on a php assignment and I have the login and logout working and displays the menu  but I am stuck on the upload of images process and still working on the others functions. For the image uploads every time I try to load an image it sends me back to the login page. Any helped would greatly appreciated. Here is more info on exactly what it needs to do.  I am working on the function picupload. Any other help or suggestion would be helpful as well as I am a little overwhelmed with all this code.

 

Once login and password are known, if the user has not requested any known course of action, the sysem responds with a form offering four choice of actions:

        1. Start a Slide show

        2. Display a listing of all the pictures to allow some or all of them to be deleted

        3. Allow the user to upload a new picture

        4. Logout from this service

 

here is my code.  also I am doing this all from one php file. Thanks

<?php

 

 

#####################################################

# GENERAL UTILITY FUNCTIONS

#####################################################

# Display a debugging message for testing the development

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('');

  }

}

 

#####################################################

# WEB SERVICE PROCESS FUNCTIONS

#####################################################

 

function loginform($u)

{

  debug("Login Form");

echo <<<messageend

<form action='loginphoto.php' method='post'>

  <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='Next'>";

}

 

function picoption()

{

  debug("picture options");

  echo "<p align=center>Not supported at this time";

  echo "<p align=center><input type='submit' value='Next'>";

}

 

function picupload()

 

{

 

echo <<<messageend

<form enctype="multipart/form-data" action="loginphoto.php" method="POST">

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

Choose a file to upload: <input name="uploadedfile" type="file" /><br />

<input type="submit" value="Upload File" />

</form>

messageend;

  }

 

function menu()

{

debug("Send menu");

echo <<<menuend

<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;

}

 

########################################

# MAIN PROCESSING SECTION

########################################

# SEND OUT HTML HEADER

echo <<<headerend

<html>

  <head>

    <title>Login Photo </title>

  </head>

  <body bgcolor="#FFCC99">

  <h1 style='font-family:sans-serif; color:brown;' align='center'>Login Photo Project</h1>

headerend;

 

# CHECK FOR NAME AND PASSWORD

 

$user = getparam('user');

$passwd = getparam('passwd');

 

##########################

# Main password table

# currently a static table

# needs further development:

#    protect the passwords

#    make the contents extendable

##########################

$logins = array(

'jane'  => 'doe',

'bill' => 'dear',

'tom'  => 'cat',

'curious' => 'george'

);

 

$state = getparam('state');

 

if (($logins[$user] == $passwd) && # IF AUTHENTICATED USER

    ($user != '') && ($passwd != ''))

{

# Set up form

echo "<form action='loginphoto.php' method='post'>\n";

 

# 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";

 

    echo "</form>\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>

 

 

This maybe easier to read.

 

<?php


#####################################################
# GENERAL UTILITY FUNCTIONS
#####################################################
# Display a debugging message for testing the development
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('');
   }
}

#####################################################
# WEB SERVICE PROCESS FUNCTIONS
#####################################################

function loginform($u)
{
   debug("Login Form");
   echo <<<messageend
<form action='loginphoto.php' method='post'>
   <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='Next'>";
}

function picoption()
{
   debug("picture options");
   echo "<p align=center>Not supported at this time";
   echo "<p align=center><input type='submit' value='Next'>";
}

function picupload()

{

echo <<<messageend
<form enctype="multipart/form-data" action="loginphoto.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
messageend;
  }
  
function menu()
{
   debug("Send menu");
   echo <<<menuend
<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;
}

########################################
# MAIN PROCESSING SECTION
########################################
# SEND OUT HTML HEADER
echo <<<headerend
<html>
  <head>
    <title>Login Photo </title>
   </head>
   <body bgcolor="#FFCC99">
   <h1 style='font-family:sans-serif; color:brown;' align='center'>Login Photo Project</h1>
headerend;

# CHECK FOR NAME AND PASSWORD

$user = getparam('user');
$passwd = getparam('passwd');

##########################
# Main password table
# currently a static table
# needs further development:
#    protect the passwords
#    make the contents extendable
##########################
$logins = array(
'jane'  => 'doe',
'bill' => 'dear',
'tom'   => 'cat',
'curious' => 'george'
);

$state = getparam('state');

if (($logins[$user] == $passwd) && # IF AUTHENTICATED USER
    ($user != '') && ($passwd != ''))
{
   # Set up form 
   echo "<form action='loginphoto.php' method='post'>\n";
   
   # 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";

    echo "</form>\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>

looked for more code for uploading images and I got it to upload if I put the upload code in separate php file but it still doest he same thing if I put it in my php function picupload.  Any help would be appreciated.

 

 


<?php


#####################################################
# GENERAL UTILITY FUNCTIONS
#####################################################
# Display a debugging message for testing the development
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('');
   }
}

#####################################################
# WEB SERVICE PROCESS FUNCTIONS
#####################################################

function loginform($u)
{
   debug("Login Form");
echo <<<messageend
<form action='loginphoto.php' method='post'>
   <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='Next'>";
}

function picoption()
{
   debug("picture options");
   echo "<p align=center>Not supported at this time";
   echo "<p align=center><input type='submit' value='Next'>";
}

function picupload()

{


//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100"); 

//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension.
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit'])) 
{
	//reads the name of the file the user submitted for uploading
	$image=$_FILES['image']['name'];
	//if it is not empty
	if ($image) 
	{
	//get the original name of the file from the clients machine
		$filename = stripslashes($_FILES['image']['name']);
	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
		$extension = strtolower($extension);
	//if it is not a known extension, we will suppose it is an error and will not  upload the file,  
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{
	//print error message
			echo '<h1>Unknown extension!</h1>';
			$errors=1;
		}
		else
		{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{
	echo "<h1>File Uploaded Successfully!</h1>";
}

echo <<<messageend
<form name="newad" method="post" enctype="multipart/form-data"  action="">
<table align="center">
	<tr><td><input type="file" name="image"></td></tr>
	<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table >	
</form>
messageend;
  }
  
function menu()
{
debug("Send menu");
echo <<<menuend
<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;
}

########################################
# MAIN PROCESSING SECTION
########################################
# SEND OUT HTML HEADER
echo <<<headerend
<html>
  <head>
    <title>Login Photo </title>
   </head>
   <body bgcolor="#FFCC99">
   <h1 style='font-family:sans-serif; color:brown;' align='center'>Login Photo Project</h1>
headerend;

# CHECK FOR NAME AND PASSWORD

$user = getparam('user');
$passwd = getparam('passwd');

##########################
# Main password table
# currently a static table
# needs further development:
#    protect the passwords
#    make the contents extendable
##########################
$logins = array(
'jane'  => 'doe',
'bill' => 'dear',
'tom'   => 'cat',
'curious' => 'george'
);

$state = getparam('state');

if (($logins[$user] == $passwd) && # IF AUTHENTICATED USER
    ($user != '') && ($passwd != ''))
{
# Set up form 
echo "<form action='loginphoto.php' method='post'>\n";

# 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";

    echo "</form>\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>


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.