Jump to content

Uploading Image to database, and then retrieving it.


michaelness

Recommended Posts

Basically I have a page called editprofile with this upload form

 

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

 

Now need the page upload.php to save that image to my database which is called "userinfo" in the row "photo"

 

this is my upload.php form

 

<?php
include 'connection.php';
echo '<link rel="stylesheet" type="text/css" href="original.css" />';

$target_path = "pics/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

 

and then after that I would like a user who is logged into be able to view that image as their profile picture

 

 

What do you need help with?  If you have a specific problem we can help you, otherwise you are just asking us to do your work for you.

 

There are a lot of PHP image upload tutorials online.  I would follow one of those.  If you encounter a specific problem, then post here and we can help.

ok, well when I click on upload picture, it copies the picture into the directory "pics" which is good. I want to get the picture name from that directory into the table "userinfo" in the row "photo"

 

This is my upload page.

 

<?php
include 'connection.php';
echo '<link rel="stylesheet" type="text/css" href="original.css" />';


//This is the directory where images will be saved


$target = "pics/";
$target = $target . basename( $_FILES['photo']['name']); 
$photo=($_POST['photo']['name']); ; 

//Writes the information to the database
$query = "UPDATE userinfo SET photo = '$photo'";
$result = mysql_query($query);


//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.";
}

?> 

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.