Jump to content

File upload retrieval


jarvis

Recommended Posts

Hi All!

I've managed to get an upload form to work but can't work out how to retrieve the images back out as I'm uploading multiple images.
I'm using the below to get the info;
[code]$first = TRUE;
        //$query = "SELECT file_name, file_type, file_size FROM uploads WHERE property_id=$pid";
        $query = "SELECT upload_id, file_name, image_description, property_id FROM uploads WHERE property_id=$pid";
        $result = mysql_query ($query);
        
        while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)){
            if($first){
            echo "<td><img src=\"uploads/{$row['file_name']}\"></td>";
            echo "<td class=\"bodytext\">File Name {$row['file_name']} </td>";
    
            $first = FALSE; [/code]

I'm opening a page specific to a property by using GET $pid. I've 2 tables one called uploads and one call properties. The images are uploaded with time stamps and a property_id to uploads and other info is entered into the properties table. Surely by referencing the property_id that was entered into uploads, I should be able to pull out the images relating to that id?
The uploads table shows entries in the db and I've an uploads folder too.

Not sure if it helps but here's the form that enters the property details and images;
[code] <?php

// Set the page title and include the HTML header.
$page_title = 'Add A Property';
require('../includes/header_logged_in.html');

$counter = 3; // Number of files to allow for.

if (isset($_POST['submit'])) { // Handle the form.

    require_once ('../../mysql_connect.php'); // Connect to the database.
    
        $message = NULL; // Create an empty new variable

    // Check for property type
    if (empty($_POST['prop_type'])) {
        $pro = FALSE;
        $message .= '<p class="error">You forgot to add a property type!</p>';
    } else {
        $pro = escape_data($_POST['prop_type']);
    }
    
    // Check for a rental price
    if (is_numeric($_POST['rent'])) {
        $r = (float) $_POST['rent'];
    } else {
        $r = FALSE;
        $message .= '<p class="error">Please enter the property\'s rental price!</p>';
    }
    
    // Check for a description - may not have one (optional)
    if (!empty($_POST['des'])) {
        $des = escape_data($_POST['des']);
    } else {
        $des = '<i>No description available!</i>';
    }

    // Check for number of rooms (optional)
    if (!empty($_POST['bed_no'])) {
        $bn = escape_data($_POST['bed_no']);
    } else {
        $bn = '<i>Number Of Bedrooms not specified!</i>';
    }

    // Check for full address
    if (empty($_POST['add1'])) {
        $ad1 = FALSE;
        $message .= '<p class="error">Please enter the full address</p>';
    } else {
        $ad1 = escape_data($_POST['add1']);
    }

    // Check for postcode.
    if (empty($_POST['pcode'])) {
        $pc = FALSE;
        $message .= '<p class="error">You forgot to enter a postcode!</p>';
    } else {
        $pc = escape_data($_POST['pcode']);
    }
    
    for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.
    
        // Create index names to refer to the proper upload and description.
        $filename = 'upload' . $i;
        $image_description = 'image_description' . $i;
    
        // Check for a file.
        if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {

            // Check for a description (not required).
            if (!empty($_POST[$image_description])) {
                $d = "'" . escape_data($_POST[$image_description]) . "'";
            } else {
                $d = 'NULL';
            }
            
            // Add the record to the database.
            $query = "INSERT INTO uploads (file_name, file_size, file_type, image_description) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $d)";
            $result = mysql_query ($query);
        
            if ($result) {
                
                // Return the upload_id from the database.
                $upload_id = mysql_insert_id();
                
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads/$upload_id")) {
                
                    echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>';
                    
                } else { // File could not be moved.
                
                    echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>';
        
                    // Remove the record from the database.
                    $query = "DELETE FROM uploads WHERE upload_id = $upload_id";
                    $result = mysql_query ($query);
                    
                    // Add more detailed error reporting, if desired.
                    
                }
                
            } else { // If the query did not run OK.
                echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
                // Print the query and invoke the mysql_error() function to debug.
            }
            
            if ($pro && $r && $ad1 && $pc) { // If all's ok...

        $query = "SELECT property_id FROM properties WHERE add1 ='$ad1'";        
        $result = @mysql_query ($query); // if no duplicate addresses
        if (mysql_num_rows($result) == 0) {
            // Produce the query    
            $query = "INSERT INTO properties (property_type, description, rent, bedrooms, add1, pcode) VALUES ('$pro', '$des', '$r', '$bn', '$ad1', '$pc')";
            
            $result = @mysql_query ($query); // Process the query
            if ($result) { // if required is supplied
            
                // Display message if all ok
                echo '<p class="parabreak"><b>Property details have been added!</b></p>';
                require('../includes/footer.html'); // Include the HTML footer
                exit(); // Terminate the script
                
            } else { // inform the user if an error occured
                $message = '<p class="error">Property details could not be added. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>';
            }        
        } else {
            $message = '<p class="error">That address already exists!</p>';
        }
        #mysql_close(); // Close the db connection

    } else {
        $message .= '<p class="parabreak">Please try again.</p>';        
    }
            
            
        } // End of if (isset($the_file)...
        
    } // End of FOR loop.
    
    mysql_close(); // Close the database connection.
        
} // End of the main Submit conditional.
// Inform the user if an error occured
if (isset($message)) {
    echo '<font color="#FFCC00">', $message, '</font>';
}
?>
<form enctype="multipart/form-data" action="add_property_new.php" method="post">

    <fieldset><legend>Fill out the form to upload a file:</legend>
    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
    <table>
            <tr>
                <td class="bodytext">Property Type: </td>
                <td><select name="prop_type">
                <option>Bungalow</option>
                <option>Chalet</option>
                <option>Flat</option>
                <option>House</option>
                <option>Studio</option>
                </select></td>
            </tr>
            <tr>
                 <td class="bodytext">Rent PCM: </td>
                <td><input type="text" name="rent" size="30" maxlength="50" value="<?php if (isset($_POST['rent'])) echo $_POST['rent']; ?>" /></td>
            </tr>
            <tr>
                <td colspan="2"><small>Please do not include the £ sign</small></td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="bodytext">Description:</td>
                <td><textarea rows="4" name="des" size="30" value="<?php if (isset($_POST['des'])) echo $_POST['des']; ?>" /></textarea></td>
            </tr>
            <tr>
                <td class="bodytext">No Of Bedrooms: </td>
                <td><select name="bed_no">
                <option>Zero</option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                </select></td>
            </tr>
            <tr>
                <td class="bodytext">Full Address:</td>            
                <td><textarea rows="4" name="add1" size="30" value="<?php if (isset($_POST['add1'])) echo $_POST['add1']; ?>" /></textarea></td>
            </tr>
            <tr>
                <td class="bodytext">Postcode:</td>
                <td><input type="text" name="pcode" size="10" maxlength="15" value="<?php if (isset($_POST['pcode'])) echo $_POST['pcode']; ?>" /></td>
            </tr>
            
            <?php // Create the inputs.
            for ($i = 0; $i < $counter; $i++) {
            
                echo '<tr><td>File:</b> <input type="file" name="upload' . $i . '" /></td>
            <td>Image Description:</b> <textarea name="image_description' . $i . '" cols="25" rows="3"></textarea></td></tr>
            ';
            }
            ?>
            <tr>
                <td></td>
            </tr>
            </table>
    </fieldset>
    <input type="hidden" name="submit" value="TRUE" />
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form>
<?php
include ('../includes/footer.html');
?> [/code]

Thanks again!

Thanks in advanced
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.