Jump to content

Help PLS


haris244808

Recommended Posts

When i open the page it shows the script  (" . $_SESSION['error'] . "

"; unset($_SESSION['error']); } ?> ) up to first name. How should i correct this.

 

 

this is the index page code

 

<?php

// Start a session for displaying any form errors

session_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Test</title>

 

</head>

 

<body>

 

<div>

<?php

if (isset($_SESSION['error']))

{

echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";

unset($_SESSION['error']);

}

?>

<form action="upload.php" method="post" enctype="multipart/form-data">

<p>

<label>First Name</label>

<input type="text" name="fname" /><br />

 

<label>Last Name</label>

<input type="text" name="lname" /><br />

 

<label>Upload Image</label>

<input type="file" name="image" /><br />

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

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

</p>

</form>

</div>

</body>

</html>

......................................

 

another question is that when i press the upload button whithout filling all the forms doesnt echo my message it says:Object not found this is the upload code i use:

 

 

<?php

// Start a session for error reporting

session_start();

 

// Call our connection file

require("includes/conn.php");

 

// Check to see if the type of file uploaded is a valid image type

function is_valid_type($file)

{

// This is an array that holds all the valid image MIME types

$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

 

if (in_array($file['type'], $valid_types))

return 1;

return 0;

}

 

// Just a short function that prints out the contents of an array in a manner that's easy to read

// I used this function during debugging but it serves no purpose at run time for this example

function showContents($array)

{

echo "<pre>";

print_r($array);

echo "</pre>";

}

 

// Set some constants

 

// This variable is the path to the image folder where all the images are going to be stored

// Note that there is a trailing forward slash

$TARGET_PATH = "images/";

 

// Get our POSTed variables

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$image = $_FILES['image'];

 

// Sanitize our inputs

$fname = mysql_real_escape_string($fname);

$lname = mysql_real_escape_string($lname);

$image['name'] = mysql_real_escape_string($image['name']);

 

// Build our target path full string.  This is where the file will be moved do

// i.e.  images/picture.jpg

$TARGET_PATH .= $image['name'];

 

// Make sure all the fields from the form have inputs

if ( $fname == "" || $lname == "" || $image['name'] == "" )

{

$_SESSION['error'] = "All fields are required";

header("Location: index.php");

exit;

}

 

// Check to make sure that our file is actually an image

// You check the file type instead of the extension because the extension can easily be faked

if (!is_valid_type($image))

{

$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";

header("Location: index.php");

exit;

}

 

// Here we check to see if a file with that name already exists

// You could get past filename problems by appending a timestamp to the filename and then continuing

if (file_exists($TARGET_PATH))

{

$_SESSION['error'] = "A file with that name already exists";

header("Location: index.php");

exit;

}

 

// Lets attempt to move the file from its temporary directory to its new home

if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))

{

// NOTE: This is where a lot of people make mistakes.

// We are *not* putting the image into the database; we are putting a reference to the file's location on the server

$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";

$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

header("Location: images.php");

exit;

}

else

{

// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to

// Make sure you chmod the directory to be writeable

$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";

header("Location: index.php");

exit;

}

?>

 

Link to comment
Share on other sites

first up, change your code to this:

<?php
            if (isset($_SESSION['error']))
            {
               echo "<span id='error'><p>{$_SESSION['error']}</p></span>";
               unset($_SESSION['error']);
            }
            ?>

second - you are applying mysql_real_escape_string() to your variables and then attempting to validate them against an empty string (which it now isn't). Either move your content check above your "sanitisation" or check it by doing the following

if ( trim(stripslashes($fname)) == "" || trim(stripslashes($lname)) == "" || trim(stripslashes($image['name'])) == "" )

Link to comment
Share on other sites

i dint implemented yet just i am trying this code separately till i make it work

 

 

index.php

 

[m]<body>


			<?php
            if (isset($_SESSION['error']))
            {
               echo "<span id='error'><p>{$_SESSION['error']}</p></span>";
               unset($_SESSION['error']);
            }
            ?>
			<form action="upload.php" method="post" enctype="multipart/form-data">
			<p>
				<label>First Name</label>
				<input type="text" name="fname" /><br />

				<label>Last Name</label>
				<input type="text" name="lname" /><br />

				<label>Upload Image</label>
				<input type="file" name="image" /><br />
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
				<input type="submit" id="submit" value="Upload" />
			</p>
			</form>

</body>[/m]

 

upload.php

[m]<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

if (in_array($file['type'], $valid_types))
	return 1;
return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$image = $_FILES['image'];

// Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( trim(stripslashes($fname)) == "" || trim(stripslashes($lname)) == "" || trim(stripslashes($image['name'])) == "" )
{
$_SESSION['error'] = "All fields are required";
header("Location: index.php");
exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
header("Location: index.php");
exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
header("Location: index.php");
exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
header("Location: images.php");
exit;
}
else
{
// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
header("Location: index.php");
exit;
}
?>
[/m]

 

 

 

andthe database connection

[m]<?php

// Input your information for the database here

// Host name
$host = "localhost";

// Database username
$username = "root";

// Database password
$password = "";

// Name of database
$database = "database_web";

$conn = mysql_connect($host, $username, $password) or die ("Could not connect");
$db = mysql_select_db($database, $conn) or die ("Could not select DB");

?>[/m]

Link to comment
Share on other sites

index.php :

<body> 
<div> 
<?php 
if (isset($_SESSION['error'])) 
{
echo"<span  id=\"error\"><p>{$_SESSION['error']}</p></span>";
unset($_SESSION['error']);  
}
?>
<form  action="upload. php"  method="post"  enctype="multipart/form-data">   
<p>
<label>First Name</label> <input type="text" name="fname" />
<br  />  
<label>Last Name</label> <input  type="text"   name="lname"    />
<br  />    
<label>Upload Image</label> <input type="file"  name="image"  />
<br  />  
<input  type="hidden" name="MAX_FILE_SIZE"  value="100000"  /> 
<input    type="submit"    id="submit"   value="Upload" /> 
</p> 
</form> 
</div> 
</body> 

and upload.php :

<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");

if (in_array($file['type'], $valid_types))
	return 1;
return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}

// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "images/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$image = $_FILES['image'];



// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( (trim(stripslashes($fname)) == "") || (trim(stripslashes($lname)) == "") || (trim(stripslashes($image['name'])) == "") )
{
$_SESSION['error'] = "All fields are required";
header("Location: index.php");
exit;
}
// Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
header("Location: index.php");
exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
$_SESSION['error'] = "A file with that name already exists";
header("Location: index.php");
exit;
}

// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
$sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
header("Location: images.php");
exit;
}
else
{
// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
// Make sure you chmod the directory to be writeable
$_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
header("Location: index.php");
exit;
}
?>

 

use that code to replace your existing one and let me know what happens.

 

Link to comment
Share on other sites

no error it looks like doesnt coonect to that page:

this is waht it shows

 

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

 

If you think this is a server error, please contact the webmaster.

 

Error 404

localhost

7/27/2011 2:57:43 PM

Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

Link to comment
Share on other sites

yes i saw that i tried but no change.

 

 

(also the first problem it is solved in IE but doesnt store the image in DB, however in forefox shows again((" . $_SESSION['error'] . "

"; unset($_SESSION['error']); } ?> ) ) even if i pres CTRL+F5)

 

driving me crazy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.