SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi everyone, I'm still having problems pulling images from database and inserting into mysql results. I am following the examples in "Handling File Uploads"...Any feedback would be appreciate...Thanks for everyone's help thus so far...especially Kevin....

    After I submit a business entry with photo, this is my result:
    "File stored successfully as C:\Uploads\965703422.gif."

    However, when I query my database this is the file entry for my Business_Logo column:
    "/tmp/php09"

    Any pointers? I don't know what I am not doing correctly....
    -------------------------------------------
    This is my code:
    ------------------------------------------
    <?php

    if ($submit): // A new business has been listed
    // using the form below.

    $dbcnx = @mysql_connect(
    "localhost", "xxxx", "xxxxx");
    mysql_select_db("xxxxxxx");

    $sql = "INSERT INTO Business SET " .
    "Business_Name='$businessname', " .
    "Category='$category', " .
    "Subcategory='$subcategory', " .
    "Neighborhood='$neighborhood', " .
    "Business_Description='$businessdescription', " .

    "Business_Logo='$uploadedfile',".
    "Expiration_Date='$expirationdate'";
    if (mysql_query($sql)) {
    echo("<P>New Business Listing Added</P>");
    } else {
    echo("<P>Error Adding New Business Listing: " .
    mysql_error() . "</P>");
    }

    if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {
    // Handle the file...
    } else {
    echo("<P>Please submit a JPEG or GIF image file.\n");
    }

    // Pick a file extension
    if ( "image/pjpeg" == $uploadedfile_type )
    $extension = ".jpg";
    else
    $extension = ".gif";

    // The complete path/filename
    $filename = "C:\\Uploads\\" . time() .
    $REMOTE_HOST . $extension;


    // Copy the file
    if (copy($uploadedfile, $filename)) {
    echo("<P>File stored successfully as $filename.");
    } else {
    echo("<P>Could not save file as $filename!");
    }





    ?>

    <P><A HREF="<?php echo($PHP_SELF); ?>">
    Add Another Business Listing</A></P>
    <P><A HREF="authors.php">Return to Authors list</A></P>

    <?php
    else: // Allow The User To Enter A New Business Listing
    ?>

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST ENCTYPE="multipart/form-data">
    <P>Enter Business Information:<BR>
    Business Logo:<INPUT TYPE=FILE NAME="uploadedfile"><br>
    Business Name:
    <INPUT TYPE=TEXT NAME="businessname" SIZE=50 MAXLENGTH=100><BR>


    <INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
    <INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE VALUE=1024>
    </FORM>

    <?php endif; ?>

    </BODY>
    </HTML>

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have already responded to your query here:
    http://www.sitepoint.com/forums/Forum4/HTML/001208.html


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Kevin,

    yes you responded, but I am following your example from your article and it is not working...so what I am I doing incorrectly?

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I thought I knew what you were saying in your previous post, so I tried something new, still did not work....Am I missing something or another procedure?

    I changed:
    "Business_Logo='$filename',".

    and the form input to:
    Business Logo:<INPUT TYPE=FILE NAME="filename"><br>

    while leaving the php codes as:

    if (mysql_query($sql)) {
    echo("<P>New Business Listing Added</P>");
    } else {
    echo("<P>Error Adding New Business Listing: " .
    mysql_error() . "</P>");
    }

    if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {
    // Handle the file...
    } else {
    echo("<P>Please submit a JPEG or GIF image file.\n");
    }

    // Pick a file extension
    if ( "image/pjpeg" == $uploadedfile_type )
    $extension = ".jpg";
    else
    $extension = ".gif";

    // The complete path/filename
    $filename = "C:\\Uploads\\" . time() .
    $REMOTE_HOST . $extension;


    // Copy the file
    if (copy($uploadedfile, $filename)) {
    echo("<P>File stored successfully as $filename.");
    } else {
    echo("<P>Could not save file as $filename!");
    }





    ?>

    <P><A HREF="<?php echo($PHP_SELF); ?>">
    Add Another Business Listing</A></P>
    <P><A HREF="authors.php">Return to Authors list</A></P>

    <?php
    else: // Allow The User To Enter A New Business Listing
    ?>

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote/font><HR>I am following your example from your article and it is not working...so what I am I doing incorrectly?<HR></BLOCKQUOTE>

    The 'example' in my article is just a bunch of snippets of code with lots of explanations of what they do. I left it up to the reader to assemble them into something that will actually work. Unfortunately, you seem to have just pasted all the snippets into your Web page in order expecting them to work.

    Let me try to explain better.

    Files get submitted by the following form field:

    &lt;input type=file name=uploadedfile&gt;

    PHP accepts the file, and stores it temporarily as /tmp/phpXX, where XX is some number chosen by PHP. That filename is stored in the variab;e $uploadedfile, so you can use it in your script. The file gets automatically deleted by PHP when the script finishes running, however, so it's up to you to put it somewhere.

    The following code picks a filename for storing the file permanently, stores it in a new variable called $filename, and finally copies the temporary file (/tmp/phpXX) to C:\UPLOADS\$filename (I'm a little confused that you seem to be mixing Unix- and Windows-style filenames... what OS are you running PHP on?):

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>// Pick a file extension
    if ( "image/pjpeg" == $uploadedfile_type )
    $extension = ".jpg";
    else
    $extension = ".gif";

    // The complete path/filename
    $filename = "C:\\Uploads\\" . time() .
    $REMOTE_HOST . $extension;

    // Copy the file
    if (copy($uploadedfile, $filename)) {
    echo("&lt;P&gt;File stored successfully as $filename.");
    } else {
    echo("&lt;P&gt;Could not save file as $filename!");
    }[/code]

    So after the above code runs, $filename contains the path/filename of the uploaded file, where it will be permanently stored. That's the filename you want to stick in your database. Your original code was inserting $uploadedfile into your database -- the temporary filename that PHP automatically deletes as soon as the script finishes running! Not only that, but your database insert code came before the code that chose the permanent filename, so you couldn't have inserted the right filename if you'd wanted to -- $filename hadn't been set yet.

    Now if those weren't problems enough, there's the matter of this code:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {
    // Handle the file...
    } else {
    echo("&lt;P&gt;Please submit a JPEG or GIF image file.\n");
    }[/code]

    This checks that the type of file that has been uploaded is either GIF of JPEG. If it is, you want your script to happily process the file as described above (picking a filename, and then inserting it into your database). If it isn't, you want your script to display a warning message and NOT insert the file into your database.

    Unfortunately, your pasted the code into your document without replacing my comment "// Handle the file..." with anything, and your put all your file handling code after the if-else statement. So what your code says is "If the file type is GIF of JPEG, do nothing. If it isn't, display a warning message. In either case, pick a filename and copy the file into permanent storage." Definitely bad!

    So let's start clean. The structure of your code should be the following:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {

    // File is GIF or JPEG.
    // Insert code here to pick a filename,
    // copy the file into temporary storage,
    // and insert the filename into your
    // database.

    } else {

    // File is of wrong type!
    // Display error message and do nothing
    // else!
    echo("&lt;P&gt;Please submit a JPEG or GIF image file.\n");

    }[/code]

    Now add the code (where it belongs!) for picking the filename and copying the file:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {

    // Pick a file extension
    if ( "image/pjpeg" == $uploadedfile_type )
    $extension = ".jpg";
    else
    $extension = ".gif";

    // The complete path/filename
    $filename = "C:\\Uploads\\" . time() .
    $REMOTE_HOST . $extension;

    // Copy the file
    if (copy($uploadedfile, $filename)) {
    echo("&lt;P&gt;File stored successfully as $filename.");
    } else {
    echo("&lt;P&gt;Could not save file as $filename!");
    }

    // Still need to insert the filename
    // into the database!

    } else {

    // File is of wrong type!
    // Display error message and do nothing
    // else!
    echo("&lt;P&gt;Please submit a JPEG or GIF image file.\n");

    }[/code]

    Finally, add the code for inserting the entry into the database. Notice that we use $filename (the permanent filename) to set the value of Business_Logo, not $uploadedfile (the file that gets deleted by PHP as soon as this script finishes running).

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>if ("image/pjpeg" == $uploadedfile_type
    or "image/gif" == $uploadedfile_type) {

    // Pick a file extension
    if ( "image/pjpeg" == $uploadedfile_type )
    $extension = ".jpg";
    else
    $extension = ".gif";

    // The complete path/filename
    $filename = "C:\\Uploads\\" . time() .
    $REMOTE_HOST . $extension;

    // Copy the file
    if (copy($uploadedfile, $filename)) {
    echo("&lt;P&gt;File stored successfully as $filename.");
    } else {
    echo("&lt;P&gt;Could not save file as $filename!");
    }

    $dbcnx = @mysql_connect( "localhost", "xxxx", "xxxxx");
    mysql_select_db("xxxxxxx");

    $sql = "INSERT INTO Business SET " .
    "Business_Name='$businessname', " .
    "Category='$category', " .
    "Subcategory='$subcategory', " .
    "Neighborhood='$neighborhood', " .
    "Business_Description='$businessdescription', " .
    "Business_Logo='$filename',".
    "Expiration_Date='$expirationdate'";

    if (mysql_query($sql)) {
    echo("&lt;P&gt;New Business Listing Added&lt;/P&gt;");
    } else {
    echo("&lt;P&gt;Error Adding New Business Listing: " .
    mysql_error() . "&lt;/P&gt;");
    }

    } else {

    // File is of wrong type!
    // Display error message and do nothing
    // else!
    echo("&lt;P&gt;Please submit a JPEG or GIF image file.\n");

    }[/code]

    I hope this makes sense to you...

    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •