/    Sign up×
Community /Pin to ProfileBookmark

Hey guys, i have a few questions, i made a form that when it is submited it saves the info in a database. Ok that was great for the first month or so of my web site but now i wanna make it better. I want to make the form so that when it is submitted it saves a picture in a folder i have set up as well as it writes the path to the picture in the database, not sure how to do that, i was wondering if you could tell me how and what i need to do in depth?

Also i want it to be displayed on the screen(not just for them but everyone on the web can see it) after they submit it, could you tell me how to do this as well?

Thanks in advance

Cody

i saw the sticky, but was wondering if i could get a little hands on help

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MegatronJun 20.2006 — i used this tutorial to setup a system that is similar to the one you're looking for

http://www.php-mysql-tutorial.com/upload-to-file-server.php

It uploads files to a dir and saves the info in a db.
Copy linkTweet thisAlerts:
@crapolauthorJun 20.2006 — thank you ?
Copy linkTweet thisAlerts:
@porkyJun 20.2006 — as start point you can use the code below


form.htm

[CODE]<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>[/CODE]


upload.php

[CODE]$path_to_folder = '/full/path/to/folder/with/images/';
$www_path_to_folder = 'http://your-domain.com/images/';

if(!empty($_POST['submit'])) {
move_uploaded_file($_FILES['image']['tmp_name'], $path_to_folder . $_FILES['image']['name']);
echo '<img src="' . $www_path_to_folder . $_FILES['image']['name'] . '">';
}[/CODE]
Copy linkTweet thisAlerts:
@crapolauthorJun 20.2006 — ok i got it to upload to a folder, and i got it to print out my info i want in the data base( first name, last name) but how do i get the data base to say the link to the picture? here is my code i have right now


here is the form

<?php

// filename: upload.form.php

// first let's set some variables

// make a note of the current working directory relative to root.

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the location of the upload handler

$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.processor.php';

// set a max file size for the html upload form

$max_file_size = 100000; // size in bytes

// now echo the html page

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<title>Upload form</title>

</head>

<body>

<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">

<h1>
Upload form
</h1>

First Name: <input type="text" name="first"><br>

Last Name: <input type="text" name="last"><br>

Zip Code: <input type="text" name="zip"><br>

E-mail Address: <input type="text" name="email"><br>

Website: <input type="text" name="web"><br>

<p>
<input type="hidden" name="pic" value="<?php echo $max_file_size ?>">
</p>

<p>
<label for="file">File to upload:</label>
<input id="file" type="file" name="file">
</p>

<p>
<label for="submit">Press to...</label>
<input id="submit" type="submit" name="submit" value="Upload me!">
</p>

</form>


</body>


</html>

here is the processor

<?php

// filename: upload.processor.php

// first let's set some variables



$username="blank";

$password="blank";

$database="blank";

$date=$_POST['date'];

$first=$_
POST['first'];

$last=$_POST['last'];

$zip=$_
POST['zip'];

$email=$_POST['email'];

$web=$_
POST['web'];

$pic=$_POST['pic'];

mysql_connect("mysql121.secureserver.net",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('',NOW(),'$first','$last','$zip','$email','$web','$pic')";

mysql_query($query);


// make a note of the current working directory, relative to root.

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

// make a note of the directory that will recieve the uploaded files

$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';

// make a note of the location of the upload form in case we need it

$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';

// make a note of the location of the success page

$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';

// name of the fieldname used for the file in the HTML form

$fieldname = 'file';



// Now let's deal with the upload

// possible PHP upload errors

$errors = array(1 => 'php.ini max file size exceeded',

2 => 'html form max file size exceeded',

3 => 'file upload was only partial',

4 => 'no file was attached');

// check the upload form was actually submitted else print form

isset($_POST['submit'])

or error('the upload form is neaded', $uploadForm);

// check for standard uploading errors

($_FILES[$fieldname]['error'] == 0)

or error($errors[$_
FILES[$fieldname]['error']], $uploadForm);

// check that the file we are working on really was an HTTP upload

@is_uploaded_file($_FILES[$fieldname]['tmp_name'])

or error('not an HTTP upload', $uploadForm);

// validation... since this is an image upload script we

// should run a check to make sure the upload is an image

@getimagesize($_FILES[$fieldname]['tmp_name'])

or error('only image uploads are allowed', $uploadForm);

// make a unique filename for the uploaded file and check it is

// not taken... if it is keep trying until we find a vacant one

$now = time();

while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))

{

$now++;

}

// now let's move the file to its final and allocate it with the new filename

@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)

or error('receiving directory insuffiecient permission', $uploadForm);

// If you got this far, everything has worked and the file has been successfully saved.

// We are now going to redirect the client to the success page.

header('Location: ' . $uploadSuccess);

// make an error handler which will be used if the upload fails

function error($error, $location, $seconds = 5)

{

header("Refresh: $seconds; URL="$location"");

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."n".

'"http://www.w3.org/TR/html4/strict.dtd">'."nn".

'<html lang="en">'."n".

' <head>'."n".

' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."nn".

' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."nn".

' <title>Upload error</title>'."nn".

' </head>'."nn".

' <body>'."nn".

' <div id="Upload">'."nn".

' <h1>Upload failure</h1>'."nn".

' <p>An error has occured: '."nn".

' <span class="red">' . $error . '...</span>'."nn".

' The upload form is reloading</p>'."nn".

' </div>'."nn".

'</html>';

exit;

} // end error handler

?>

and here is how it is displayed in the end

<?

$username="blank";

$password="blank";

$database="blank";

mysql_connect("mysql121.secureserver.net",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM contacts";

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Sumfight Street Team</center></b><br><br>";

$i=0;

while ($i < $num) {

$date=mysql_result($result,$i,"date");

$first=mysql_result($result,$i,"first");

$last=mysql_result($result,$i,"last");

$zip=mysql_result($result,$i,"zip");

$email=mysql_result($result,$i,"email");

$web=mysql_result($result,$i,"web");

$pic=mysql_result($result,$i,"pic");

echo "<center><b>$date</b><br><b>$first $last</b><br>Zip: $zip<br>E-mail: $email<br>Web: $web<br>Picture Link: $pic<br></center><hr><br>";

$i++;

}

?>
×

Success!

Help @crapol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...