haris244808 Posted July 27, 2011 Share Posted July 27, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/ Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 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'])) == "" ) Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247793 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 Thank you for replying Moddy However i tried the first suggestion but still same problem ... second suggestion doesnt worked too... if u have any other idea pls tell me otherwise thak you very much for help Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247812 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 any time you make changes to your code, even if you still get the same result, could you please post the revised code up so we can see how it fits with the rest of the stuff, php tags should be put around any code that you post - this makes it much easier to read (and is also part of the forum rules) Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247813 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247816 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 I was using firefox now i opened with IE and doesnt show the error but the second problem is still same: when i press upload button: Object was not found any idea? Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247820 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247825 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 ok i opened with IE and the first problem is solved: (firefox was showing itself) what about the second own why doesnt echo my message when i lef blank the boxes? Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247827 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 after replacing the upload.php what message are getting instead of your "All Fields...." P.S. ctrl+F5 force refreshes - saves caching probles like you had with firefox. Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247830 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247831 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 sorry, there is an extra space in the <FORM ACTION= that I posted it reads action="upload. php" should obviously be as you had it action="upload.php" without the space. Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247834 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247842 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 ok, this is getting weird. Could you attach your actual .php files from your server for both index.php and upload.php so I can have a look at the raw code? Also, I asume that upload.php is in the same directory on the same server as index.php is? Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247872 Share on other sites More sharing options...
haris244808 Posted July 27, 2011 Author Share Posted July 27, 2011 hej man sorry for losing your time.. I found the problem.. i had saved index .html and because of that i faced with these problems again thank you very much... i will try to add multiple image upload now if you have any suggestion i will be very thankful Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247875 Share on other sites More sharing options...
Muddy_Funster Posted July 27, 2011 Share Posted July 27, 2011 Search the forum, there are a lot of examples and snipits for that kind of thing. Quote Link to comment https://forums.phpfreaks.com/topic/242921-help-pls/#findComment-1247894 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.