SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    ********* Scotland Saltire's Avatar
    Join Date
    Jan 2000
    Location
    Scotland
    Posts
    660
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload and rename images with ID of new record

    Hi Folks,

    I'm working on a Realty website just now, where photos are going to be uploaded to the server, then the URL pulled from the databasel

    I can work out the pulling from the database no bother, the thing I am having problems with is renaming the images to the ID for the record I am creating with the data submission form.

    For the record, the column for the ID is called "ID" and the URL for the picture will be called "photourl"

    Any help is very much appreciated!

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the file will be uploaded with the name you give the field in the form (just like any other form field). Then you copy the file to the place you want to store it and in so doing you rename it as well. Eg:

    Lets say your form has this field:

    <input type="file" name="userfile">

    Then your script will do this:
    PHP Code:
    // assume you have captured the id for the image/DB record in var $id

    $saveto '/path/to/save/to' $id '.jpg';
    copy($userfile$saveto); 
    If $id was "123" then the image would now be saved as "123.jpg".
    http://www.php.net/manual/en/features.file-upload.php

  3. #3
    ********* Scotland Saltire's Avatar
    Join Date
    Jan 2000
    Location
    Scotland
    Posts
    660
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers mate.

  4. #4
    ********* Scotland Saltire's Avatar
    Join Date
    Jan 2000
    Location
    Scotland
    Posts
    660
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I have this, but it still isn't working. I have picked through the code but can't seem to see anything wrong.

    Any ideas?

    <body>
    <?php
    // Add a listing to the database
    ?>
    <form action="http://localhost:9000/sgr/add.php" method="post" enctype="multipart/form-data">
    <p>Property Address:
    <textarea name="address" rows="5" cols="40"></textarea>
    <br>
    Description:
    <textarea name="description" rows="5" cols="40"></textarea>
    <br>
    Features:
    <textarea name="features" rows="5" cols="40"></textarea>
    </p>
    <input type="file" name="userfile">
    <p>
    <input type="Submit" name="submitnew" value="Submit">
    </p>
    </form>
    <?php

    // Connect to the database
    $dbconnect = @mysql_connect(
    "localhost", "root", "");
    if (!$dbconnect) {
    echo( "Unable to connect");
    exit();
    }
    // Select the database
    if (! @mysql_select_db("sgr") ) {
    echo( "Unable to locate the db");
    exit();
    }
    // Request the ID of the record about to be created to
    // assist in renaming the image file
    $propertyselect = mysql_query("SELECT ID FROM property");
    while ($propertyselection = mysql_fetch_array($propertyselect)) {
    $id = $propertyselection["ID"];
    $var = "propertyselection$id";
    if ($$var) {
    // assume you have captured the id for the image/DB record in var $id
    $extension = ".jpg";
    $saveto = "C:\\nusphere\\apache\\nsdocs\\sgr\\images\\" . $id . $extension;
    copy($userfile, $saveto);
    // If a property has been submitted add it to the database
    $add = "INSERT IGNORE INTO property SET " .
    "Address='$address', " .
    "Description='$description', " .
    "Features='$features'
    $ID=$id";
    // Verify addition of new property listing
    $ok = mysql_query($add);
    if ($ok) {
    echo("<p>Added</p>");
    } else {
    echo("<p>Error</p>");
    }
    }
    }
    ?>
    </body>

  5. #5
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I can't help you, but I have some advice: edit that message and enclose the code with [p h p] and [/p h p].

    (Take the spaces out. They're there so that and isn't in php code. Q. Is there anyway to cancel the vB Code (like with a slash or anything)?
    Last edited by ucahg; Jun 25, 2001 at 07:48.

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you getting any error messages? Also, are you sure the if condition is evaluating to true? Otherwise the code looks ok - at least the bit where you are messing with the file.

    It could be something to do with your settings - I have no idea of PHP on Windoze . However, safe_mode should be set to off in php.ini (which I think it should be by default with the nusphere install). See you're messing around with the filesystem here - so I don't know how all that works with Apache/PHP on Win.

  7. #7
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $saveto = "C:\\nusphere\\apache\\nsdocs\\sgr\\images\\" . $id . $extension;

    on windows needs to be (assuming the script is in 'sgr')
    $saveto="images/$id.$extension";

  8. #8
    ********* Scotland Saltire's Avatar
    Join Date
    Jan 2000
    Location
    Scotland
    Posts
    660
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas why this isn't inserting any data into the database now?

    PHP Code:
    <?php
    // Request the ID of the record about to be created to 
    // assist in renaming the image file
    $lastinsert mysql_insert_id();
    {
    $newinsert $lastinsert 1;
    }
    if ($
    $newinsert) {
    $extension ".jpg";
    $saveto "images/$newinsert.$extension";
    copy($userfile$saveto);
    // If a property has been submitted add it to the database
    $add "INSERT IGNORE INTO property SET " .
    "Address='$address', " .
    "Suburb='$insertsuburb', " .
    "Description='$description', " .
    "Features='$features', " .
    "Price='$price'";
    // Verify addition of new property listing
    $ok mysql_query($add);
    if (
    $ok) {
    echo(
    "<p>Added</p>");
    } else {
    echo(
    "<p>Error</p>");
    }
    }
    ?>

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_insert_id()

    I am suspicious of this. Has there been an insert query prior to your call to mysql_insert_id(). If not, it will return 0. If it returns 0 then $newinsert will have a value of 1. If there is already a record with primary key of 1, then your INSERT IGNORE query will fail silently.

    But that is just a guess. The best thing to do when debugging is to librally sprinkle your code with echo statements echoing out variables to the browser. That way you can see where the unexpected behaviour is occuring.

    eg:
    PHP Code:
    $lastinsert*=*mysql_insert_id();
    echo 
    "<br>lastinsert : $lastinsert<br>";

    ...

    $add "INSERT blah, blah ...";
    echo 
    "<br>add : $add<br>"
    BTW, I see you are picking up the lingo. Get a dog up ya!

  10. #10
    ********* Scotland Saltire's Avatar
    Join Date
    Jan 2000
    Location
    Scotland
    Posts
    660
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no record which has just been inserted before the mysql_insert_id() is called, so you are correct in saying it wull be trying to insert with a primary key of 1, which is already there so is failing.

    How else would I do it so that it would get the number of the next ID to be inserted and so be used to rename the image being uploaded?

    I know I may seem like a bit of a drongo, but hey, I'm trying!

  11. #11
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like "SELECT id from table WHERE 1=1 ORDER BY id DESC LIMIT 0, 1" to get the last id inserted.


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
  •