Hey all For an image gallery (made a topic for some java errors before too) i have for friends and family i let them upload images and they'll be added to the gallery automatically. I'm using a script for that which i found. After testing it out a bit i noticed it doesn't remove spaces in file names. That's really annoying obviously I do know str_replace will take care of it, but i'm clueless where or what to change. Here's the piece of code i THINK it might have to be added to. //set url variable $domst = ""; $drecks = "/"; $imgf = $fupl.$HTTP_POST_FILES['userfile']['name']; $thbf = $tpath.$imgf; $urlf = $domst .$domain .$drecks .$path .$imgf; PHP: Any ideas or do you need more code?
I dont know what the $fupl variable is but you're basically taking what the users give you. Which is kinda bad. But this should be what you want/need. $img_name = $HTTP_POST_FILES['userfile']['name']; $img_name = str_replace(" ","_",$img_name) ; $imgf = $fupl.$img_name ; Just replace the line $imgf = $fupl.$HTTP_POST_FILES['userfile']['name']; with the three above. A quick explanation would be : str_replace("what to replace","what to replace it with", "the string") ;
Thanks a lot i'll give it a try right now I'll edit Edit: Ouch Now i get this: Warning: imagecreatefromjpeg(images/430151361sta____r.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in Warning: imagesx(): supplied argument is not a valid Image resource in Warning: imagecopyresampled(): supplied argument is not a valid Image resource in Warning: imagedestroy(): supplied argument is not a valid Image resource in Code (markup): The $fupl = $zufall = rand(1,9999999999); $fupl = "$zufall"; PHP: And what do you mean with this?
Use ereg_replace it will remove all invalid chars, eg. Try... $imgf = ereg_replace("[^A-Za-z0-9.]", "", $imgf); Code (markup): after [COLOR=#0000FF]$imgf[/COLOR] = [COLOR=#0000FF]$fupl[/COLOR].[COLOR=#0000FF]$HTTP_POST_FILES[/COLOR][COLOR=#66CC66][[/COLOR][COLOR=#FF0000]'userfile'[/COLOR][COLOR=#66CC66]][/COLOR][COLOR=#66CC66][[/COLOR][COLOR=#FF0000]'name'[/COLOR][COLOR=#66CC66]][/COLOR]; Code (markup):
Ok, this is what's happening. The code snippet you posted acts on the file and what we should have done is move or rename the file AFTER everything has finished. Mainly its just an issue with timing the actions. I'll post some code when I get up. The last part is the usual caveat when doing web based programming. The mantra is never trust user input (validate what they give you to make sure its will not damage the server its running on). This is partly why sites offering file uploads almost always rename uploaded files. I know this is for friends/family but its a good idea to follow.
Thanks for the replies Trying Danny's method and awaiting the help from Shallowink Will report and edit. Edit: Same result from Danny's method. Must be because of reasons Shallowink said. i'll look it up on google some more too.
ok here's a start... http://www.phpeasystep.com/workshopview.php?id=18 Its a file upload example with the uploaded file being renamed. Problem is I'm pretty sure the script you have is moving the file (which is why it has the path info etc) so either we rename it before hand or wait till the scripts completed all of its actions.
It's hurting my brain. Took a look at that page (thanks) but it doesn't help me out in the slightest way. It does give me the idea that i didn't show enough code: //generate random number $zufall = rand(1,9999999999); $fupl = "$zufall"; $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl .$HTTP_POST_FILES['userfile']['name']); if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else { ?> <? //set url variable $domst = ""; $drecks = "/"; $img_name = $HTTP_POST_FILES['userfile']['name']; $imgf = $fupl.$img_name; $thbf = $tpath.$imgf; $urlf = $domst .$domain .$drecks .$path .$imgf; PHP: Tried replacing some things, adding some, but i keep getting the same error. I did find out that if i put $img_name = str_replace(" ","_",$img_name) ; PHP: Before $img_name = $HTTP_POST_FILES['userfile']['name']; PHP: then nothing happens. File gets uploaded, but no strings replaced. So i guess it has to be there.
You are 110% correct. the section we need to work with is here: $fupl = "$zufall"; $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl .$HTTP_POST_FILES['userfile']['name']); if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else { ?> PHP: //generate random number $zufall = rand(1,9999999999); $fupl = "$zufall"; $img_name = $HTTP_POST_FILES['userfile']['name']; $img_name = str_replace(" ","_",$img_name) ; $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl .$img_name); if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else { ?> <? //set url variable $domst = ""; $drecks = "/"; $imgf = $fupl.$img_name; $thbf = $tpath.$imgf; $urlf = $domst .$domain .$drecks .$path .$imgf; PHP: Hopefully the script only uses $imgf from this point out else it will cause errors in other parts of the script. Maybe it will work.
And it works perfectly Thanks a lot, Shallowink. Next time i harras you people with questions i'll just remember to post more code +rep for your help. Edit: Used Danny's method in yours too Which should avoid more wrong characters in the future (or so i hope) New: //generate random number $zufall = rand(1,9999999999); $fupl = "$zufall"; $img_name = $HTTP_POST_FILES['userfile']['name']; $img_name = ereg_replace("[^A-Za-z0-9.]", "", $img_name); $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl .$img_name); if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else { ?> <? //set url variable $domst = ""; $drecks = "/"; $imgf = $fupl.$img_name; $thbf = $tpath.$imgf; $urlf = $domst .$domain .$drecks .$path .$imgf; PHP: Great help from both of you.