Jump to content

php and mysql user pics


Jiraiya

Recommended Posts

Allow them to upload a file. When they upload it assign it the same id number as their profile. Or maybe edit their mysql row to have a column referencing the image they upload. Then just for their profile do a query and select that column and do an <img src= to show the image.

Link to comment
Share on other sites

You're better off storing the images to a folder on your server and then inserting the path as a variable into your database.

 

Its as simple as outputting the path within an image tag thereafter.

 

I can post an example if you want to go down this road.

 

Otherwise I think you need to us BLOB rather than VARCHAR (could be wrong).

Link to comment
Share on other sites

so far i got this script, and im not really sure were to go from here, i already had googled for tutorials on this and they didnt help much.

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Upload a profile picture: <input name="uploaded pic" type="file" /><br />
<input type="submit" value="Upload File" />
</form>


</body>
</html>

Link to comment
Share on other sites

This should work fine as an example. Copy it, paste it, view it, edit it (you should only need to remove the code relating to the second and third file uploads) and let me know.  :)

 

<?php
if (isset($_POST['submit']))

{ 
//CHECK IF UPLOAD FILE FIELD 1 IS EMPTY

        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];

        if($filesize1==0) 
        {
        $filesize1=FALSE;
        $message_filesize1="You forgot to enter this image file!";
        //echo "$message_filesize1";
        }
        else 
        {
        $filesize1=TRUE;
        }

//CHECK IF UPLOAD FILE FIELD 2 IS EMPTY

        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];

        if($filesize2==0) 
        {
        $filesize2=FALSE;
        $message_filesize2="You forgot to enter this image file!";
        //echo "$message_filesize2";
        }
        else 
        {
        $filesize2=TRUE;
        }

//CHECK IF UPLOAD FILE FIELD 3 IS EMPTY

        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

        if($filesize3==0) 
        {
        $filesize3=FALSE;
        $message_filesize3="You forgot to enter this image file!";
        //echo "$message_filesize3";
        }
        else 
        {
        $filesize3=TRUE;
        }

//CHECK IF ANY THE FILES ARE THE SAME:

        $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0];
        $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1];
        $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2];

        $filesize1=$HTTP_POST_FILES['ufile']['size'][0];
        $filesize2=$HTTP_POST_FILES['ufile']['size'][1];
        $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

        if 
          (
               (($filenamecheck1 == $filenamecheck2) ||  ($filenamecheck2 == $filenamecheck3)  || ($filenamecheck1 == $filenamecheck3))
               && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit'])))
          )
            {
             $filenamecheck_message = "one ore more of the files are the same";
             //echo "$message_filenamecheck";
             $message_filenamecheck = TRUE;
             $filenamecheck1= FALSE;
             $filenamecheck2= FALSE;
             $filenamecheck3= FALSE;
            }  

        else 
           {
            $message_filenamecheck = FALSE;
            $filenamecheck1= TRUE;
            $filenamecheck2= TRUE;
            $filenamecheck3= TRUE;         
           }

//CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF

       $filesize1=$HTTP_POST_FILES['ufile']['size'][0];

      if   (   (
           ($HTTP_POST_FILES['ufile']["type"][0] == "image/gif")
           || ($HTTP_POST_FILES['ufile']["type"][0] == "image/jpeg")
           || ($HTTP_POST_FILES['ufile']["type"][0] == "image/pjpeg")
         )
        // &&($HTTP_POST_FILES['ufile']["size"][0] < 50000)
      )

      { 
       $filetypecheck1=TRUE;
      }

      else
       {
      if (!($filesize1 == 0))
       {
       $filetypecheck1=FALSE;
       $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       //echo "$message_filetypecheck1";
       }
      }

//CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF

      $filesize2=$HTTP_POST_FILES['ufile']['size'][1];

      if   (   (
           ($HTTP_POST_FILES['ufile']["type"][1] == "image/gif")
           || ($HTTP_POST_FILES['ufile']["type"][1] == "image/jpeg")
           || ($HTTP_POST_FILES['ufile']["type"][1] == "image/pjpeg")
         )
        // &&($HTTP_POST_FILES['ufile']["size"][1] < 50000)
      )

      { 
       $filetypecheck2=TRUE;
      }

      else
      {
       if (!($filesize2 == 0))
      {
       $filetypecheck2=FALSE;
       $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!";
       //echo "$message_filetypecheck2";
      }
      }

//CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF

      $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

      if   (   (
           ($HTTP_POST_FILES['ufile']["type"][2] == "image/gif")
           || ($HTTP_POST_FILES['ufile']["type"][2] == "image/jpeg")
           || ($HTTP_POST_FILES['ufile']["type"][2] == "image/pjpeg")
         )
        // &&($HTTP_POST_FILES['ufile']["size"][2] < 50000)
     )

     { 
      $filetypecheck3=TRUE;
     }

     else
     {
      if (!($filesize3 == 0))
      {
      $filetypecheck3=FALSE;
      $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!";
      //echo "$message_filetypecheck3";
      }
      }


//CHECK IF FILE 1 EXISTS ALREADY
    
     $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
     $filesize1=$HTTP_POST_FILES['ufile']['size'][0];

     if (
        (file_exists($path1)) && 
        (!($filesize1 == 0))
       )
      {
        $fileexistcheck1=FALSE;
        $message_fileexistcheck1="The first file already exist on the server!";
        //echo "$message_fileexistcheck1";
      }
     else
      {
        $fileexistcheck1=TRUE;
      }

//CHECK IF FILE 2 EXISTS ALREADY
    
     $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
     $filesize2=$HTTP_POST_FILES['ufile']['size'][1];

     if (
        (file_exists($path2)) && 
        (!($filesize2 == 0))
       )
      {
        $fileexistcheck2=FALSE;
        $message_fileexistcheck2="The second file already exist on the server!";
        //echo "$message_fileexistcheck2";
      }
     else
      {
        $fileexistcheck2=TRUE;
      }

//CHECK IF FILE 3 EXISTS ALREADY
    
     $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
     $filesize3=$HTTP_POST_FILES['ufile']['size'][2];

     if (
        (file_exists($path3)) && 
        (!($filesize3 == 0))
       )
      {
        $fileexistcheck3=FALSE;
        $message_fileexistcheck3="The third file already exist on the server!";
        //echo "$message_fileexistcheck3";
      }
     else
      {
        $fileexistcheck3=TRUE;
      }
  
//IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED
       		
if ($filesize1 
      && $filesize2 
      && $filesize3 
      && $filenamecheck1 
      && filenamecheck2 
      && filenamecheck3 
      && $filetypecheck1 
      && $filetypecheck2 
      && $filetypecheck3 
      && $fileexistcheck1 
      && $fileexistcheck2 
      && $fileexistcheck3)

	{             
                  $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
                  $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
                  $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];

                  copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
                  copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
                  copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);


                  include("dbinfo.php");
          mysql_connect(localhost,$username,$password);
          @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database.");

                  $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
                  $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
                  $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];

$query = "INSERT INTO test VALUES ('','','','','','','','','','','','','','','',NOW(),'','$path1','$path2','$path3')";
mysql_query($query);

//NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED


                  echo "The following image files have been uploaded:</br></br>
                  ";

                  echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; 
                  //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]." KB<BR/>"; 
                  echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; 
                  //echo "<img src=\"$path1\" height=\"150\">";
                  echo "<P>";

                  echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; 
                  //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; 
                  echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; 
                  //echo "<img src=\"$path2\" height=\"150\">";
                  echo "<P>";

                  echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; 
                  //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; 
                  echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; 
                  //echo "<img src=\"$path3\" height=\"150\">";

               exit();

	}
} 
?>

<HTML>
<HEAD>
</HEAD>
<BODY>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>

<?php
// IF ANY INDIVIDUAL INPUT FIELD IS DEFINED AS FALSE THEN DISPLAY ERROR MESSAGE 
if ($message_filesize1  || $message_filesize2 || $message_filesize3 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filenamecheck || $message_filetypecheck1 || $message_filetypecheck2 || $message_filetypecheck3 || $message_fileexistcheck1 || $message_fileexistcheck2 || $message_fileexistcheck3) 
echo '*This item could not be processed because some of the information is missing or incorrect.</br></br>'; ?>
   
<ol>
    <li>
    <label for "ufile[]">Image File 1:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <?php if ($message_filesize1) echo ''.$message_filesize1.''; ?>
    <?php if ($message_filetypecheck1) echo ''.$message_filetypecheck1.''; ?> 
    <?php if ($message_fileexistcheck1) echo ''.$message_fileexistcheck1.''; ?>
    </li>


    <li>
    <label for "ufile[]">Image File 2:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <?php if ($message_filesize2) echo ''.$message_filesize2.''; ?>
    <?php if ($message_filetypecheck2) echo ''.$message_filetypecheck2.''; ?> 
    <?php if ($message_fileexistcheck2) echo ''.$message_fileexistcheck2.''; ?>
    </li>

    <li>
    <label for "ufile[]">Image File 3:</label>
    <input type="file" name="ufile[]"  id="ufile[]"/>
    <?php if ($message_filesize3) echo ''.$message_filesize3.''; ?>
    <?php if ($message_filetypecheck3) echo ''.$message_filetypecheck3.''; ?> 
    <?php if ($message_fileexistcheck3) echo ''.$message_fileexistcheck3.''; ?>
    </br>
    <?php if ($filenamecheck_message) echo ''.$filenamecheck_message.''; ?>
    </li>
    
    <li>
    <input name="submit" type="submit">
    </li>
</ol>
</fieldset>
</form>	
</BODY>
</HTML>

 

This can be done a lot neater using arrays for the error messages. Its just a newb approach.

Link to comment
Share on other sites

The code is set up so that it stores it in a folder called upload. You need to create this folder in the same path as your form for starters. Also make sure that this folder is writable to by the public.

 

This section of the provided code determines where it goes:

 

<?php

//REPLACE THE WORD UPLOAD WITH YOUR FOLDER NAME

                  $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
                  $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
                  $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];

//NO NEED TO EDIT BELOW BUT THATS THE CODE THAT ACTUALLY COPIES THE FILE TO THE FOLDER

                  copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
                  copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
                  copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);

Link to comment
Share on other sites

Ah, I forgot about that.

 

I created a file called dbinfo.php that contains my details related to database connection. If you have these details in your form already it ok. Its just a neater way of going about it.

 

It looks like this:

 

<?php
$host = 'localhost'; //usually localhost
$username = 'XXX'; //your username assigned to your database
$password = 'XXX'; //your password assigned to your user & database
$database = 'XXX'; //your database name
?> 

 

Is your database created and can you connect to it?

 

 

Link to comment
Share on other sites

yea i can connect to my database, im having trouble editing the code so it only allows one picture to be uploaded and not three and im also having trouble figureing out were im supposed to store it. am I supposed to store it in a sub directory? because i created one called "uploads"

Link to comment
Share on other sites

The folder should be situated in whatever folder your storing your php page in.

 

If your php is in your root directory (where your home page is) then this is where the upload folder should be.

 

If your php page is in a folder then so should your upload folder.

 

Whenever I try a tutorial I create a database according to the tutorial requirements rather than my own initially. Try creating a database with one index and three varchar fields for the paths.

 

You'll need to modify the insert statement as well.

 

Once your satisfied you can upload the three image files begin working your way backwards and edit the code to facilitate only one upload.

 

I'll be logged in for a while in any case.

 

 

Link to comment
Share on other sites

ok i have fixed it so it uploads pics, know my question is how to display one picture that a user uploads and then have it display on the users profile?

 

Really depends on your script. You'd need to post an example of what you're working with. Do you know how to ouput an image? (apologies if i'm incorrectly assuming you're a newb)

Link to comment
Share on other sites

First create a file: output.php

 

Modify the following example:

 

<?
include("dbinfo.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM yourtablename";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$imageurl1=mysql_result($result,$i,"imageurl1");
$imageurl2=mysql_result($result,$i,"imageurl2");
$imageurl3=mysql_result($result,$i,"imageurl3");


echo "
<img src='$imageurl1' width='250' height='188'> </br>
<img src='$imageurl2' width='250' height='188'> </br>
<img src='$imageurl3' width='250' height='188'> </br>
</br>
<hr>
</br>
     ";

$i++;
}

?>

 

Again this is based on outputting three image files. Just an example but a starting point.

 

 

Link to comment
Share on other sites

im really confused about about how to display a picture that a logged in user has submitted onto there profile. I have it set up were it will upload the picture to a folder, but i dont understand how im supposed to display that picture if it isnt a variable data in a table??

Link to comment
Share on other sites

Its a two way process. First the image is copied to the folder (which you have done). Next you need to write the path (url) by inserting it into your database e.g. upload/imagename.jpg When outputting you need to define your output as a variable and place it as the path within an image e.g. img src='$imageurl' which in turn will call on the image you previosly uploaded.

 

I recommend researching how to upload and output images adeptly first. A generous amount of tutorials are sprinkled around ye old web. You'll need to start looking at sessions and basic login tutorials thereafter.

 

Don't make the mistake of trying to nail it all in your very first script.  ;)

 

> Learn how to insert path (url) into table

> Learn how to output the image by calling its path

> Learn how to work with sessions and logins

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.