michaelmcgurk
michaelmcgurk

Reputation: 6509

Replace previous image and add new image to server with PHP

I'm currently working on an 'Edit Project' script that allows users to replace images on my site. This part works great.

However, I also want the option to allow new images to be uploaded - these do not replace existing ones. So, in summary, the user can replace existing images AND upload new images using the form.

Question: How do I tell my PHP that some images are new and not replacement ones? I need to do this so I can run INSERT INTO queries on the new images.

I've attached a portion of my code below:

PHP to process replacement of existing files:

    // get original name for ProjectImage update
    $query3 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
    $result3 = mysql_query ($query3); // Run the Query
    $row3 = mysql_fetch_array($result3);
    $originalname = $row3[1];

    $number_of_file_fields = 0;
    $number_of_uploaded_files = 0;
    $number_of_moved_files = 0;
    $uploaded_files = array();
    $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory

    for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
        $number_of_file_fields++;
        if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
            $number_of_uploaded_files++;
            $uploaded_files[] = $_FILES['images']['name'][$i];
            if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                $number_of_moved_files++;
                $url = $_FILES['images']['name'][$i];
                list($width, $height, $type, $attr) = getimagesize("uploaded/".$_FILES["images"]['name'][$i]);
                $pieces = explode("_", $url);
                $ImageName = $pieces[1];    
                mysql_query("UPDATE ProjectImage SET Name = '$ImageName', Url = '$url', UrlHeight =  '$height' WHERE Name = '$originalname' AND Pid = '$id'");
            }    
        }    
    }

&

PHP used to generate list of existing images:

$query2 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
$result2 = mysql_query ($query2); // Run the Query
while ($row2 = mysql_fetch_array($result2))
{
?>
<div id="file_container" class="control-group">
    <label for="image" class="control-label">Image:</label>
    <div class="controls">
        <input name="images[]" type="file"  />
        <img src="uploaded/<?php echo $row2['Url']; ?>" />
        <a href="delete.php?id=<?php echo $row2['id']; ?>">Delete</a>
        <br />
    </div>
</div>
<?php
}
?>
<a href="javascript:void(0);" onClick="add_file_field();">Add another</a><br />

Many thanks for any pointers :-)

Upvotes: 1

Views: 6229

Answers (1)

Robin Castlin
Robin Castlin

Reputation: 10996

I'm not fully sure if you want them to replace existing or not. Anyhow, I'll give you all requried methods to do either:

file_exists() - Returns boolean based on path. Can be used to check if same file already exists or not.

rename() - You probably have this somewhere in your code already. It basicly replaces existing files on relocation. If you don't want this behavior, use the file_exists() condition first.

You could easily solve the upload and replace feature through the MYSQL INSERT ... ON UPDATE method. This would require to have a column to the imagepath set as unique, which you should have regardless.

Example:

INSERT INTO ProjectImage (Name, Url, UrlHeight, path_to_file)
VALUES ('$ImageName', '$url', '$height', '$path_to_file')
ON DUPLICATE KEY UPDATE Name = VALUES(Name), Url = VALUES(Url), UrlHeight = VALUES(UrlHeight)

Appart from that, I'm afraid I find your code too unlogical to work with. Some variables have Capital letters, some are too long for what they describe and your code is in danger of MySQL injections.

Upvotes: 1

Related Questions