Hi i was wondering if anyone could help.... I have made a little script that puts whatever a user enters into a mysql database. As well as the forms my script has an image input that uploads an image to my webserver. The thing i'm stuck on is getting the script to input the name of the file into a field in the database.
So basically the file will be uploaded and when the submit button is clicked a link to that image will be generated and put into the database.
I've put my code below, i'm guessing i need a variable that will take the name of the file and add it to a pre defined url for example http://www.mysite.com/images/$file_name, then insert that along with everything else when i run the insert query? Also, how would i go about converting the filename into something radomly generated? So that it would stop any existing files being overwritten?
[PHP]<?php
if (isset($_POST['submitted'])) {
$name = mysql_escape_st ring(trim($_POS T['name']));
$address = mysql_escape_st ring(trim($_POS T['address']));
$postcode = mysql_escape_st ring(trim($_POS T['postcode']));
$telephone = mysql_escape_st ring(trim($_POS T['telephone']));
$email = mysql_escape_st ring(trim($_POS T['email']));
$picture = mysql_escape_st ring(trim($_POS T['picture']));
// This bit puts the file in the $file variable into the folder on the server.
if ($file_name !="") {
copy ("$file", "/var/www/vhosts/server.com/httpdocs/uploader1/$file_name")
or die ("Sorry there was a problem");}
$dbid = mysql_connect ('localhost', 'address', 'pass');
mysql_select_db ('addresses',$d bid)
or die ("Cannot find database");
$query = "INSERT INTO `book` (`aid`, `name`, `address`, `postcode`, `telephone`, `email`) VALUES ('', '$name', '$address', '$postcode', $telephone, '$email')";
$result = mysql_query($qu ery,$dbid)
or die("INSERT error:".mysql_e rror());
echo 'Row inserted and image uploaded';
exit;
}
?>
<html>
<head>
<title>Data submission - db version</title>
</head>
<body>
<form enctype="multip art/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input name="name" /><br />
Address: <input name="address" /><br />
Postcode: <input name="postcode" /><br />
Telephone: <input name="telephone " /><br />
Email: <input name="email" /><br />
Select Picture: <input type="file" name="file" size"80"><br />
<br /><input type="submit" name="submitted " value="Submit" >
</form><br />
</body>
</html> [/PHP]
So basically the file will be uploaded and when the submit button is clicked a link to that image will be generated and put into the database.
I've put my code below, i'm guessing i need a variable that will take the name of the file and add it to a pre defined url for example http://www.mysite.com/images/$file_name, then insert that along with everything else when i run the insert query? Also, how would i go about converting the filename into something radomly generated? So that it would stop any existing files being overwritten?
[PHP]<?php
if (isset($_POST['submitted'])) {
$name = mysql_escape_st ring(trim($_POS T['name']));
$address = mysql_escape_st ring(trim($_POS T['address']));
$postcode = mysql_escape_st ring(trim($_POS T['postcode']));
$telephone = mysql_escape_st ring(trim($_POS T['telephone']));
$email = mysql_escape_st ring(trim($_POS T['email']));
$picture = mysql_escape_st ring(trim($_POS T['picture']));
// This bit puts the file in the $file variable into the folder on the server.
if ($file_name !="") {
copy ("$file", "/var/www/vhosts/server.com/httpdocs/uploader1/$file_name")
or die ("Sorry there was a problem");}
$dbid = mysql_connect ('localhost', 'address', 'pass');
mysql_select_db ('addresses',$d bid)
or die ("Cannot find database");
$query = "INSERT INTO `book` (`aid`, `name`, `address`, `postcode`, `telephone`, `email`) VALUES ('', '$name', '$address', '$postcode', $telephone, '$email')";
$result = mysql_query($qu ery,$dbid)
or die("INSERT error:".mysql_e rror());
echo 'Row inserted and image uploaded';
exit;
}
?>
<html>
<head>
<title>Data submission - db version</title>
</head>
<body>
<form enctype="multip art/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input name="name" /><br />
Address: <input name="address" /><br />
Postcode: <input name="postcode" /><br />
Telephone: <input name="telephone " /><br />
Email: <input name="email" /><br />
Select Picture: <input type="file" name="file" size"80"><br />
<br /><input type="submit" name="submitted " value="Submit" >
</form><br />
</body>
</html> [/PHP]
Comment