tfoster Posted March 27, 2006 Share Posted March 27, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/ Share on other sites More sharing options...
litebearer Posted March 27, 2006 Share Posted March 27, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21024 Share on other sites More sharing options...
tfoster Posted March 27, 2006 Author Share Posted March 27, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21027 Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21034 Share on other sites More sharing options...
tfoster Posted March 27, 2006 Author Share Posted March 27, 2006 [!--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. :) Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21036 Share on other sites More sharing options...
shoz Posted March 27, 2006 Share Posted March 27, 2006 When displaying the image name etc you can do something similar to the following.[code]<?phpif ($_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. Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21048 Share on other sites More sharing options...
tfoster Posted March 27, 2006 Author Share Posted March 27, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/5890-overwrite-image-file/#findComment-21243 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.