/    Sign up×
Community /Pin to ProfileBookmark

Image Upload Form

I search this forum and couldn’t find any info on how to upload an image using PHP. I want to create an interface for a user to upload his/her pic and add some info along with that pic. Then save that info to a directory. Finally, after they add their pic and info I want to store that info to an page(i.e. their profile) something similar to myspace. I’m new to this so any amount of help would be great. Thanks!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SheldonNov 21.2005 — something like this registration form, uploads an image and other forms, then resizes the image to what you set and adds the information to a mysql database.

Of course you will need to mod it, but i have just finished writing it so there might be a fews bugs because as i said i have just written it.


register.php
[code=php]

<?php ob_start();
session_start();//start session.
include('common/connect.php'); //include connection to the databse script
include('common/top.php'); //include the top.php file for the layout
$page = "register"; // name te page, use a different name for each page.



//once the user has entered in their details, add to the database and upload/resize the image.
if($_GET['signup'] == "start") {


$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$perms = $_POST['perms'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['comments'];

$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['password2'] = $_POST['password2'];
$_SESSION['perms'] = $_POST['perms'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['phone'] = $_POST['phone'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['comments'] = $_POST['comments'];

//start checking for errors
if($_POST['name'] == "" || $_POST['username'] == "" || $_POST['password'] == "" || $_POST['phone'] == "" || $_POST['comments'] == "" || $_POST['email'] == "") {

//make sure all required forms are filled in.
//some feilds are empty
header("Location: register.php?error=empty_feilds");

} else {

if($_POST['password'] == $_POST['password2']) {

//continue because passwords match
//check for duplicate usernames

$sql1 = "SELECT * FROM artist WHERE username = '{$_POST['username']}'";
$sql1 = mysql_query($sql1);
$result1 = mysql_fetch_assoc($sql1);

if($_POST['username'] == $result1['username']) {

header("Location: register.php?error=username");

}else{


//UPLOADS FILE TO SERVER
$UploadDir = 'images/artist/';
$UploadFileName = $UploadDir . $_FILES['userfile']['name'];
$FilePath = 'images/artist/' . $_FILES['userfile']['name'];
$thefile = $_FILES['userfile']['name'];


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $UploadFileName)) {

include("image_resize.php");
$image = resizeImage($thefile,$UploadDir,149);

$sql="INSERT INTO artist(username,password,perms,name,phone,mobile,address,email,photo,comments)
VALUES('$username','$password','$perms','$name','$phone','$mobile','$address','$email','$image','$comments')";
if(mysql_query($sql)) {




include('success/register.php');

session_destroy();

} else {

echo("ERROR IN THE SQL QUERY");
echo(mysql_error());
}
} else {

header("Location: register.php?error=photo");
}

}

}else{



//passwords do not match

//print error
header("Location: register.php?error=password");

}



}//end error checking




//finish the processing section of the form.





} else { //this closes the proccessing section of the page




//if they havnt submitted the form print the form then!


?>
<SCRIPT language=JavaScript>
<!--

function validate(f){
if (f.agree.checked == false )
{
alert('You must have Read and Agree to the Terms and Conditions to continue.');
return false;
}else{
//do nothing
}
}
//-->
</SCRIPT>


<div class="content">
<span id="title">Register</span>

<p>Registration will allow you to add your <b>Artwork - Exhibition</b> or list a <b>Gallery</b>.<br>
<span align="center"><a href="about.php"><u>All this is FREE</u></a></span>
You may upload pictures with text discriptions, advise people of up coming exhibitions, also add your Gallery details.<br><br>
People who view <b>Nuglass gallery</b> will be able to see your work and make contact with you direct.</p>
<p>
<form method="post" enctype="multipart/form-data" action="register.php?signup=start" name="regs" onsubmit="return validate(this)">
<input type="hidden" name="perms" value="2">
<fieldset class="fieldset">
<?php if($_GET['error'] == "empty_feilds") { print '<div class="red">Some Required Feilds were left empty.</div>'; } ?>
<label for="name">Name<span class="red">*</span>:</label>
<input type="text" name="name" id="" size="30" value="<?php print($_SESSION['name']); ?>" tabindex="1">
<br>
<label for="username">Username<span class="red">*</span>:<?php if($_GET['error'] == "username") { print '<span class="red">This username is already in use</span>'; } ?></label>
<input type="text" name="username" id="username" size="30" value="<?php print($_SESSION['username']); ?>" tabindex="2">
<br>
<label for="password">Password<span class="red">*</span>:<?php if($_GET['error'] == "password") { print '<span class="red">Both Passwords must Match</span>'; } ?></label>
<input type="password" name="password" id="password" size="30" value="" tabindex="3">
<br>
<br>
<label for="password">Retype Password<span class="red">*</span>:</label>
<input type="password" name="password2" id="password2" size="30" value="" tabindex="4">
<br>
<label for="email">Email<span class="red">*</span>:</label>
<input type="text" name="email" id="email" size="30" value="<?php print($_SESSION['email']); ?>" tabindex="5">
<br>
<label for="phone">Phone<span class="red">*</span>:</label>
<input type="text" name="phone" id="phone" size="30" value="<?php print($_SESSION['phone']); ?>" tabindex="6">
<br>
<label for="mobile">Mobile:</label>
<input type="text" name="mobile" id="mobile" size="30" value="<?php print($_SESSION['mobile']); ?>" tabindex="7">
<br>
<label for="addres">Address:</label>
<textarea name="address" id="address" cols="27" rows="6" tabindex="8"><?php print($_SESSION['address']); ?></textarea>
<br>
<label for="photo">Artist Photo and Discription<span class="red">*</span>:<?php if($_GET['error'] == "photo") { print '<span class="red">There was an error uploading your photo, Please check the filetype and try again.<br>Please remember to use only lowercase charactors.</span>'; } ?></label>
<br>
<span style="font-size:x-small;"><i>Please use <b>.jpg</b><b>.gif</b> or <b>.png</b> file types.</span>
<br>
<input type="file" name="userfile" id="userfile" size="30" tabindex="9"><br>
<textarea name="comments" id="comments" cols="27" rows="6" tabindex="10"><?php print($_SESSION['comments']); ?></textarea>
<br>
<input type="checkbox" value="0" name="agree" id="agree">I have read and agree to the <a href="terms.php">Terms and Conditions</a> of this site<span class="red">*</span>.
<br>
<label for="submit"><span class="red">*</span> Compulsory</label>
<input type="submit" name="submit" id="submit" value="Register">
<br>
</fieldset>
</form>
</p>
</div>

<?php
}

//finish off the page
include('common/menu.php');
include('common/bottom.php');

?>

[/code]


image_resize.php (is included and needed in the script).

[code=php]

<?php

function resizeImage($filename,$path,$limitWidth) {

list($initWidth, $initHeight) = getimagesize($path.$filename);

if($initWidth > $limitWidth) {
$per = sprintf('%01.4f',$limitWidth / $initWidth);
$newwidth = sprintf('%01.0f',$per * $initWidth);
$newheight = sprintf('%01.0f',$per * $initHeight);

set_time_limit(3000);
ini_set("memory_limit","30M");

$image_type = strstr($filename, '.');
switch(strtolower(strtoupper($image_type))) {
case '.jpg':
$source = imagecreatefromjpeg($path.$filename);
break;
case '.png':
$source = imagecreatefrompng($path.$filename);
break;
case '.gif':
$source = imagecreatefromgif($path.$filename);
break;
default:
echo("Error Invalid Image Type");
die;
break;
}

$fullpath = $path . $filename;
list($width, $height) = getimagesize($path.$filename);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $fullpath, 100);
$filepath = $fullpath;
}

return $filepath;
}

?>

[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsNov 21.2005 — php.net has a tutorial on how to do this.
Copy linkTweet thisAlerts:
@kylixauthorNov 21.2005 — Thanks so much!
×

Success!

Help @kylix 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.18,
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,
)...