User uploading pictures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C.Joseph Drayton

    User uploading pictures

    Hi All,

    I have a web site I am developing, and have a question. I would like
    members to be able to upload pictures.

    Do you think they should be saved as individual files or should they be
    put in an MySQL database? Which would you recommend, and do you have any
    sample code for accomplishing this?

    Ciao . . . C.Joseph

    And on the seventh day God said,
    "I will rest . . . Murphy take over."
  • Chris - SyracuseCS.com

    #2
    Re: User uploading pictures

    Here ya go. This is the script that I made for one of my sites. The
    form takes the userid, and if the image gets uploaded ok it gets
    renamed to useridu.jpg (100u.jpg) I only allow jpg's for this script.
    It then gets inserted in an images table as unapproved (0). After I
    approve the images the image gets renamed to userid.jpg (100.jpg) and
    the users profile gets updated with the new picture.


    Upload form

    <?php

    echo "Upload an image<br><br>";
    echo "
    <form action=\"upload er.php\" method=\"post\"
    enctype=\"multi part/form-data\">

    <input type=\"file\" name=\"file\" size=\"50\">
    <br/>
    <input type=\"submit\" value=\"Upload File\">
    <input type='hidden' name='userid' value='$userid' >
    </form>[ <a href=users.php class=\"menulin k\">Back</a> ]<br><br>";

    ?>

    uploader.php

    <?php
    global $userid;
    if($file_name !="")
    {
    $image_type = strstr($file_na me, '.');

    if($image_type == ".jpg"){
    $file_name = $userid."u.jpg" ;
    copy ("$file", "/www/htdocs/user_images/$file_name") or die("Could
    not copy file");

    include "connect.ph p";
    $result = mysql_query("IN SERT INTO images (userid,name) VALUES
    ('$userid','$fi le_name')") or die ("Error in insert sql:".
    mysql_error());
    include "disconnect.php ";

    include("mem_he ader.php");
    echo "Your file has been uploaded!<br><b r>";
    echo "<br>Your photo is pending approval<br><br >";
    echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=users.php\" >";
    include("mem_fo oter.php");
    }else{
    include("mem_he ader.php");
    echo "Incorrect File Type. Your file must be a JPG<br><br>";
    echo "Your file has NOT been uploaded!<br><b r>";
    echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
    URL=users.php?c mg=Upload\">";
    include("mem_fo oter.php");
    }

    }
    else {
    include("mem_he ader.php");
    echo "No file specified<br><b r>";
    echo "Your file has NOT been uploaded!<br><b r>";
    echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
    URL=users.php?c mg=Upload\">";
    include("mem_fo oter.php");
    }


    ?>

    Comment

    • C.Joseph Drayton

      #3
      Re: User uploading pictures

      Chris - SyracuseCS.com wrote:[color=blue]
      > Here ya go. This is the script that I made for one of my sites. The
      > form takes the userid, and if the image gets uploaded ok it gets
      > renamed to useridu.jpg (100u.jpg) I only allow jpg's for this script.
      > It then gets inserted in an images table as unapproved (0). After I
      > approve the images the image gets renamed to userid.jpg (100.jpg) and
      > the users profile gets updated with the new picture.
      >
      >
      > Upload form
      >
      > <?php
      >
      > echo "Upload an image<br><br>";
      > echo "
      > <form action=\"upload er.php\" method=\"post\"
      > enctype=\"multi part/form-data\">
      >
      > <input type=\"file\" name=\"file\" size=\"50\">
      > <br/>
      > <input type=\"submit\" value=\"Upload File\">
      > <input type='hidden' name='userid' value='$userid' >
      > </form>[ <a href=users.php class=\"menulin k\">Back</a> ]<br><br>";
      >
      > ?>
      >
      > uploader.php
      >
      > <?php
      > global $userid;
      > if($file_name !="")
      > {
      > $image_type = strstr($file_na me, '.');
      >
      > if($image_type == ".jpg"){
      > $file_name = $userid."u.jpg" ;
      > copy ("$file", "/www/htdocs/user_images/$file_name") or die("Could
      > not copy file");
      >
      > include "connect.ph p";
      > $result = mysql_query("IN SERT INTO images (userid,name) VALUES
      > ('$userid','$fi le_name')") or die ("Error in insert sql:".
      > mysql_error());
      > include "disconnect.php ";
      >
      > include("mem_he ader.php");
      > echo "Your file has been uploaded!<br><b r>";
      > echo "<br>Your photo is pending approval<br><br >";
      > echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=users.php\" >";
      > include("mem_fo oter.php");
      > }else{
      > include("mem_he ader.php");
      > echo "Incorrect File Type. Your file must be a JPG<br><br>";
      > echo "Your file has NOT been uploaded!<br><b r>";
      > echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
      > URL=users.php?c mg=Upload\">";
      > include("mem_fo oter.php");
      > }
      >
      > }
      > else {
      > include("mem_he ader.php");
      > echo "No file specified<br><b r>";
      > echo "Your file has NOT been uploaded!<br><b r>";
      > echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
      > URL=users.php?c mg=Upload\">";
      > include("mem_fo oter.php");
      > }
      >
      >
      > ?>
      >[/color]
      Hi Chris,

      Then I am assuming that you are recommending that I save individual
      files.

      I will try your script this afternoon and let you know how it works out.

      Thank you so much for the recommendation.

      Ciao . . . C.Joseph

      And on the seventh day God said,
      "I will rest . . . Murphy take over."

      Comment

      Working...