Jump to content


Photo

overwrite image file?


  • Please log in to reply
6 replies to this topic

#1 tfoster

tfoster
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 12:08 AM

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!

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 27 March 2006 - 12:17 AM

Have you tried deleting the file?

[a href=\"http://www.tizag.com/phpT/filedelete.php\" target=\"_blank\"]http://www.tizag.com/phpT/filedelete.php[/a]

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 tfoster

tfoster
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 12:22 AM

[!--quoteo(post=358712:date=Mar 26 2006, 07:08 PM:name=lila)--][div class=\'quotetop\']QUOTE(lila @ Mar 26 2006, 07:08 PM) View Post[/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?

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 27 March 2006 - 01:15 AM

When I closed the page and re-opened it, the picture had change. Is this just a database lag?

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.

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";

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"?
echo '<input type="text" name="inputProdPic" value="'.$row['products_pic'].'" />';



#5 tfoster

tfoster
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 01:20 AM

[!--quoteo(post=358725:date=Mar 26 2006, 08:15 PM:name=shoz)--][div class=\'quotetop\']QUOTE(shoz @ Mar 26 2006, 08:15 PM) View Post[/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"?
echo '<input type="text" name="inputProdPic" value="'.$row['products_pic'].'" />';
[/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. :)


#6 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 27 March 2006 - 02:30 AM

When displaying the image name etc you can do something similar to the following.
<?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>';
}
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.

#7 tfoster

tfoster
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 04:40 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users