Jump to content

overwrite image file?


tfoster

Recommended Posts

Hello!

I am using move_uploaded_file to upload a picture. The pictures are associated with certain products, so the naming scheme makes the filename [primary key].gif. My problem is that when I try to change the picture, it will not overwrite the first picture in the directory being uploaded to. Any ideas? Thanks for any help!
Link to comment
Share on other sites

[!--quoteo(post=358712:date=Mar 26 2006, 07:08 PM:name=lila)--][div class=\'quotetop\']QUOTE(lila @ Mar 26 2006, 07:08 PM) [snapback]358712[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello!

I am using move_uploaded_file to upload a picture. The pictures are associated with certain products, so the naming scheme makes the filename [primary key].gif. My problem is that when I try to change the picture, it will not overwrite the first picture in the directory being uploaded to. Any ideas? Thanks for any help!
[/quote]

When I closed the page and re-opened it, the picture had change. Is this just a database lag? And I have one more problem. If I try to just display what is in the database in this cell, it won't display properly. The filename doesn't come up. I have checked my code to make sure I am accessing it correctly. If I just echo it, it shows up okay, but it won't do it in the confines of this statement:
echo "<td><input type=file name=inputProdpic height=20 width=20 value=\"" . $rowProd[products_pic] . "\"></td>\n";

Any ideas?
Link to comment
Share on other sites

[quote]
When I closed the page and re-opened it, the picture had change. Is this just a database lag?
[/quote]
Your browser may have been using the previous version of the image stored in its cache. When doing tests for something like this, you should empty your cache(temporary internet files) to force the browser to download the image from the server.
[quote]
The filename doesn't come up. I have checked my code to make sure I am accessing it correctly. If I just echo it, it shows up okay, but it won't do it in the confines of this statement:
echo "<td><input type=file name=inputProdpic height=20 width=20 value=\"" . $rowProd[products_pic] . "\"></td>\n";
[/quote]
I don't think the "file" type of an input element is allowed an author inputted value for the "value" attribute. I believe that it's a security precaution. Perhaps you meant it to be "text"?
[code]
echo '<input type="text" name="inputProdPic" value="'.$row['products_pic'].'" />';
[/code]

Link to comment
Share on other sites

[!--quoteo(post=358725:date=Mar 26 2006, 08:15 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Mar 26 2006, 08:15 PM) [snapback]358725[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Your browser may have been using the previous version of the image stored in its cache. When doing tests for something like this, you should empty your cache(temporary internet files) to force the browser to download the image from the server.
[/quote]
That makes sense.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
I don't think the "file" type of an input element is allowed an author inputted value for the "value" attribute. I believe that it's a security precaution. Perhaps you meant it to be "text"?
[code]
echo '<input type="text" name="inputProdPic" value="'.$row['products_pic'].'" />';
[/code]
[/quote]

The problem with changing it to text is that this statement serves dual purposes. It allows the user to add a new product picture, or edit an existing product picture. If I change it to text, it will not allow the file upload. I thought about putting in a if statement so it would be text when you edit the product, but if the user wants to overwrite the picture, I am denying them the ability. Hmmm... I think I am in a catch 22. :)
Link to comment
Share on other sites

When displaying the image name etc you can do something similar to the following.
[code]
<?php
if ($_POST['submit'])
{
    if (isset($_FILES['edit_pics']) && is_array($_FILES['edit_pics']))
    {
        //handle pics for editing here
        foreach ($_FILES['edit_pics']['name'] as $pic_id=>$name)
        {
            print $pic_id.':'.$name."<br />";
            /**
             * You can verify here that the pic_id is a number and that the
             * user is authorized to change the file before doing
             * "move_uploaded_file" etc
             */
            
        }
    }
    if (isset($_FILES['new_pic']))
    {
        /**
          * here you deal with a new file that has been uploaded
          */
    }
}
else
{
    echo '<td><input type="file" name="edit_pics[10]" /></td>';
    echo '<td><input type="file" name="edit_pics[15]" /></td>';
    echo '<td><input type="file" name="new_pic" /></td>';
}
[/code]
Note the keys "10" and "15" in the example above would be the pic_ids of the different pictures. You can have one "submit" button to handle all the changes using something similar to the above.

PHP.net has an example of [a href=\"http://www.php.net/manual/en/features.file-upload.multiple.php\" target=\"_blank\"]handling multiple file uploads[/a].

If you have any trouble post what you've tried.
Link to comment
Share on other sites

Hey Shoz!

Thank you so much for the help! I ended up going a little different way with it. Instead of utilizing file variables, I just set another variable where a picture already existed called prodpic that was a text input, like this:
if(empty($rowProd[products_pic]))
echo "<td><input type=file name=inputProdpic height=20 width=20 value=\"" . $rowProd
[products_pic] . "\"></td>\n";
else{
echo "<td><input type=text name=prodpic value=\"" . $rowProd[products_pic] . "\">\n<br><input
type=file name=inputProdpic height=20 width=20 value=\"" . $rowProd[products_pic] . "\"></td>\n";
}

Then, on update, I set it up like this:
if(!empty($inputProdpic))
$updateProd = "update PRODUCTS set category_no='" . $editCat . "', products_name='" .
$inputProdname . "', products_desc='" . $inputProddescription . "', products_avail='" . $inputProdavail . "',
products_pic='" . $inputProdpic . "', products_group='" . $inputProdgroup . "' where products_no = " .
$editProd;
else
$updateProd = "update PRODUCTS set category_no='" . $editCat . "', products_name='" .
$inputProdname . "', products_desc='" . $inputProddescription . "', products_avail='" . $inputProdavail . "',
products_pic='" . $prodpic . "', products_group='" . $inputProdgroup . "' where products_no = " .
$editProd;

This makes two text boxes when you are updating a product, so it is definitely not the most streamlined, but it works...

Again, thanks for your hlep!
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.