Jump to content

Assigning a picture upload via mysql and php


ProcalX

Recommended Posts

Hi all, creating a MYSQL, PHP & XHTML site designed to support local rugby clubs. Just putting the final touches to the functionality now so thanks for your help so far.

 

I would like to provide site administrators with the ability to assign photos to a members profile when initially registering their account, I have no experience of dealing with what presumably will be a function that will upload a photo to a location and then linking it some how to data in the database.

 

Thanks for your help,

 

Tom

Hi there,

 

Image upload via PHP is a very simple thing, and easy to do as well.

 

The client-side setup for it, requires a form with an input type of "file", eg. <input type="file=" name="image">.

In addition to that, you need to specify the enctype  type of form as well, eg. <form method="post" enctype="multipart/form-data">.

 

The back-end of it is also simple, you can use the $_FILES global for this: which is an array containing information about the uploaded file.

You would typically move the uploaded image to a directory of your choosing, as it saves it by default in the PHP temporary folder.

After that, you would just add a column to the database, such as "image_url", and then assign the image file name to it.

 

There are many great tutorials for this, and many methods as well. Just do a Google search for tutorials.

I have tried this:

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

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

	$check1 = mysql_query("SELECT * FROM rh_users WHERE username='$username'");
	if (mysql_num_rows($check1)>0)
	{
		echo "The username '$username' already exists, choose an alternative.";
	}
	else
	{
		mysql_query("INSERT INTO rh_users (id,isadmin,username,password,firstname,surname,email,day,month,year,posts,joined,number,position,subteam,height,weight,image) VALUES ('','$isadmin','$username','$password','$firstname','$surname','$email','$day','$month','$year','0',NOW(),'$shirtnumber','$position','$subteam','$height','$weight','$pic')");
		/* Write photo to the server */
		if (move_uploaded_file($_FILES['image']['tmp_name'], $target))
		{
			/* Check */
			echo "The file ". basename( $_FILES['uploadedfile']['username']). "
			has been uploaded, and your information has been added to the directory.";

			echo "Registration succesful. <a href='admin_cp.php'>Click here to return to the Admin Control Panel</a>.";
		}
		else
		{
			echo "Sorry, there was a problem uploading your file.";
		}
	}

 

But recieve errors:

Notice: Undefined index: username in C:\xampp\htdocs\redhawks\admin_registeruser2.php on line 56

Notice: Undefined index: username in C:\xampp\htdocs\redhawks\admin_registeruser2.php on line 60

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\redhawks\admin_registeruser2.php on line 71

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpD6CA.tmp' to 'images/profiles' in C:\xampp\htdocs\redhawks\admin_registeruser2.php on line 71
Sorry, there was a problem uploading your file. 

 

:shrug:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.