Ninjakreborn Posted December 27, 2006 Share Posted December 27, 2006 [code]<?php$file1 = $_FILES['file1']; if (isset($_POST['file1_name'])) { $name1 = mysql_real_escape_string($_POST['file1_name']); }else { $name1 = $file1['name']; }?>[/code]The above code was working, until I added the isset post, and let him name it himself. He wanted the abilitiy to choose the image name if he wanted to.For now, I need the name. I want to figure out how to retain the extension, whether it's .jpg, .jpeg, .gif, .bmp or whatever he wants to upload. The question I would need to ask though now is, is that a good idea, letting him name them, or just leave them named the way they were when he originally uploaded them.And let the name just be an addition that it can show on. Quote Link to comment Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 As long as you have a form field named "file1_name" in the form it will always be set upon submitting, you need to check if it's empty or not. so isset() always returns true while empty() determines if it contains data or not[code]<?phpif (!empty($_POST['file1_name'])){// user filled it in with something}?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 27, 2006 Share Posted December 27, 2006 A better way to check if a form field is emtpy is to use:[code]<?phpif (strlen(stripslashes(trim($_POST['file1_name']))) != 0) {//// field is filled in//}?>[/code]When you use the emtpy() function, it will return true if the value of the field is "0".Ken Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted December 27, 2006 Author Share Posted December 27, 2006 I am not worried about that, that always has worked, if (isset( has always worked for me, I will test it out a little better, but when it's not filled in it ignores it, when it is filled in it doesn't so far.What I was more of asking, if I take the name of a filefilename.jpg for instancehe wants to be able to manaully create teh name, when I create the name of the image what he choses it doesn't save the extension. I want the extension to be there, say if he wants to name it dave. and it was a bmp it'sdave.bmp would be hte nameif it's a jpg then dave.jpg, right now it's just naming the file dave.Which isn't good. Quote Link to comment Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 [quote author=businessman332211 link=topic=120045.msg492143#msg492143 date=1167239001]I am not worried about that, that always has worked, if (isset( has always worked for me, I will test it out a little better, but when it's not filled in it ignores it, when it is filled in it doesn't so far.[/quote]Test this out then:[code]<?phpif(isset($_POST['submit'])){if(isset($_POST['testfield'])) echo "Testfield IS set";if(empty($_POST['testfield'])) echo " but it IS infact empty (or value '0')"; else echo " and it is NOT empty: {$_POST['testfield']}";}?><form method="post" action=""><input type="text" name="testfield" value="" /><input type="submit" name="submit" value="test" /></form>[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted December 27, 2006 Author Share Posted December 27, 2006 Ah, ok that makes sense now. Thanks, that is something new I will keep in mind.Very strange, it always seemed to work right, I may have to redo some older stuff now, thanks i will keep that in mind.How about the name, I want to get hte filename to keep the extension when doing that.But it's overwriting the namefilename.extwith just the newname of what he watned, I watned to keep the extension. Quote Link to comment Share on other sites More sharing options...
alpine Posted December 27, 2006 Share Posted December 27, 2006 one quick example of how to do it with filetype validation:[code]<?php$ok_file_types = array('image/pjpeg' => 'jpg','image/jpeg' => 'jpg','image/gif' => 'gif',);$uploaded = $_FILES['file1'];$filetype = $uploaded['type'];if(array_key_exists($uploaded['type'], $ok_file_types)){$extention = $ok_file_types[$filetype];if(!empty($_POST['file1_name'])){$filename = $_POST['file1_name'].".".$extention;}else{$filename = $uploaded['name'];}}else{ echo "illegal filetype uploaded";}?>[/code]or without, something like this:[code]<?php$uploaded = $_FILES['file1'];if(!empty($_POST['file1_name'])){$extention = explode(".", $uploaded['name']);$filename = $_POST['file1_name'].".".$extention[1];}else{$filename = $uploaded['name'];}?>[/code]None is tested though... Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted December 27, 2006 Author Share Posted December 27, 2006 Thanks that atleast points me in the right direction, thanks for taking the time to teach me that a minute ago, adn for helping me with my initial problem, thanks again. Quote Link to comment 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.