PDA

View Full Version : i need a help for this php code


imeshSJ
07-27-2009, 01:03 PM
"<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
mysql_select_db("Database_Name") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>"
me code eke thiyenne eka photo ekak upload wela sql eke write karana eka. i need a code for upload three photos

viraj_slk
07-27-2009, 01:50 PM
machn mama php walata very new. but i did a simple uploader recently. is this what you are looking for?

-------------------------------<?php

/**
* @author 'me'
* @copyright 2009
*/


define("MAX_FILE_SIZE", 1048000);

//Funciton to print blank rows.
function br(){ print "<br />";}

if ( isset($_FILES['fupload']) ){
print "<p> Thanks for the upload.. processing your file now.. </p>";
br();

$file_type = $_FILES['fupload']['type'];
if ( ( $file_type == "image/jpeg") || ($file_type == "image/pjpeg") || ($file_type == "image/gif") ) {

//To check if the file size exceeds the specified limit..
if ($_FILES['fupload']['size'] > MAX_FILE_SIZE)
{
print "Your file size exceed the maximum specified size of ".MAX_FILE_SIZE;
exit;
}

//Now we just print some info. about the file to the screen
print "file name: ".$_FILES['fupload']['name']; br();
print "file size: ".$_FILES['fupload']['size']; br();
print "file type: ".$_FILES['fupload']['type']; br();
print "file tmp_name(temp path): ".$_FILES['fupload']['tmp_name']; br();

//Specifying source and the target (destination) for our file
$source = $_FILES['fupload']['tmp_name'];
$target = "upload/".$_FILES['fupload']['name'];

//Moving file to the destination
move_uploaded_file($source, $target);

//Prints the image according to its original height and width
$img_size = getImageSize($target);
$imgstr = "<p> <img src=\"$target\" width=\"$img_size[0]\" ";
$imgstr .= " height=\"$img_size[1]\" alt = \"uploaded image here\" /> </p>";

//Display the successfully uploaded image
print $imgstr;
}
else{
//Show error message
print "Your file type ".$_FILES['fupload']['type']." cannot be proecessed. <br> Please upload as .jpeg or .gif";
}
}
else{
#If the user arrives on the web page for the first time -> then '$_FILES['fupload'])' is not set
print "Welcome. Please upload any &nbsp; ( .jpg/.jpeg/.gif ) &nbsp; picture files."; br();
}


?>

<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>File uploader</title>
</title>
<body>

<form enctype="multipart/form-data"
action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p>
<input type="file" name="fupload" /><br/>
<input type="submit" value="upload!" />
</p>
</form>

</body>
</html>


this is pretty simple code to let user upload images. when the upload is successful, you can write something into your database. I guess something like the location of the image within your server.

be careful when you let people upload images or whatever. that's why i check whether the files are really in image format. Still it's better if you can replace some things in the file names with underscore.

for example, a very popular way of shell upload (eg. c99 shell ) is to rename the php file to something like pretty.php.jpg

when you upload this to some servers, some servers try to process the php code that's really in the file. but some servers do not do that. so its good if you can extract the file name, which in this case is 'pretty.php' and replace any unnecessary characters such as '.' with underscores. there is a better tool to check if uploaded files are really image files. i can't remember it now. google is your friend :)

Hope this helps

imeshSJ
07-27-2009, 03:23 PM
machan meke pro ekak thiyanawa
meka single fil nemata onne pic 5k wath eka para
e wage,a e add una ewa sql deta bese ek write wennath onne

mawella
07-27-2009, 03:30 PM
mata nikan kalanthewage :# meka dekala :#

imeshSJ
07-27-2009, 03:36 PM
wathura tikak bipan

viraj_slk
07-28-2009, 02:29 PM
machan meke pro ekak thiyanawa
meka single fil nemata onne pic 5k wath eka para
e wage,a e add una ewa sql deta bese ek write wennath onne

hm.. i understand.
am havin sm wrk to do so didnt hv time to try any more. but i am thinking maybe to get an array and go through it. 5 upload sections html eke daana eka simple ne. eeta passe ewa tika php eken array ekakata gunna samahara wita puluwun wey. if i get a chance to try later i'l post here