petenaylor Posted February 23, 2012 Share Posted February 23, 2012 Hi all I need to remove the spaces in an uploaded file. I have tried str_replace and trim etc... but it doesn't work. Here is my code: move_uploaded_file($_FILES['image']['tmp_name'][$i], WEB_UPLOAD."/images/galleries/g".$gid."/".$_FILES['image']['name'][$i]) or die("Error uploading image "); Thanks Pete Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/ Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2012 Share Posted February 23, 2012 Where is the whitespace? In the filename? Is it leading and trailing whitespace, or in the middle of the string? Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320549 Share on other sites More sharing options...
petenaylor Posted February 23, 2012 Author Share Posted February 23, 2012 It is in the middle of the string so the file is 'gardening 1.jpg' Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320552 Share on other sites More sharing options...
Zane Posted February 23, 2012 Share Posted February 23, 2012 I need to remove the spaces in an uploaded file. I have tried str_replace and trim etc... but it doesn't work. it doesn't work? How so? Those methods are exactly how you get rid of those spaces. Show us your code Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320557 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2012 Share Posted February 23, 2012 Then str_replace should work. $filename = str_replace(' ', '', $_FILES['userfile']['name']); Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320558 Share on other sites More sharing options...
petenaylor Posted February 23, 2012 Author Share Posted February 23, 2012 That's great, what would be the best way to add str_replace to this code: move_uploaded_file($_FILES['image']['tmp_name'][$i], WEB_UPLOAD."/images/galleries/g".$gid."/".$_FILES['image']['name'][$i]) or die("Error uploading image "); Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320561 Share on other sites More sharing options...
Zane Posted February 23, 2012 Share Posted February 23, 2012 Eventhough putting all of that information on one line will work, it is a good practice, in the beginning/intermediate stages of programming, to break it all apart into variables. This will make it much easier for you to debug. It doesn't hurt to space out your concatenations either...well, it may increase filesize, but I have a feeling your script isn't that big to begin with. $filename = $_FILES['image']['tmp_name'][$i]; $dest = WEB_UPLOAD . "/images/galleries/g" . $gid . "/" . $_FILES['image']['name'][$i]; move_uploaded_file($file, $dest) or die("Error uploading image "); Now it shouldn't be too difficult to figure out where to use str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320575 Share on other sites More sharing options...
3raser Posted February 24, 2012 Share Posted February 24, 2012 Zane, are you saying we should or shouldn't make everything on one line? Isn't it more professional/neater code? Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320593 Share on other sites More sharing options...
Zane Posted February 24, 2012 Share Posted February 24, 2012 Isn't it more professional/neater code? There is nothing neat or professional about the code below. move_uploaded_file($_FILES['image']['tmp_name'][$i], WEB_UPLOAD."/images/galleries/g".$gid."/".$_FILES['image']['name'][$i]) or die("Error uploading image "); Professional ... and neat... code would be readable. The above code would be fine the way it is if you didn't want to strip the whitespace, but you do. Even though a professional coder may look at move_up..... and str_repl... and realize what's going on, you still have an abundance of single quotes, concatenation dots, slashes, and plenty of other things that could accidentally be deleted upon debug. Zane, are you saying we should or shouldn't make everything on one line? I'm not saying to NEVER put everything on one line. My point is, that you came to a forum asking where to put str_replace in your own code (I assume it's your own code at least). There's is nothing neat about having no clue where to put that function. Take what I said and use your best judgement. Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320598 Share on other sites More sharing options...
3raser Posted February 24, 2012 Share Posted February 24, 2012 Thank you for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/257649-trim-whitespace-from-uploaded-file/#findComment-1320600 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.