JayLewis Posted March 16, 2007 Share Posted March 16, 2007 This is the function i use to upload images to my server in a certain directory... how would i change the file name on upload? thanks. <? $dir=opendir("image/upload"); $i=0; while($imgfile=readdir($dir)) { if ($imgfile != "." && $imgfile!="..") { $imgarray[$i]=$imgfile; $i++; } } closedir($dir); $rand=rand(0,count($imgarray)-1); if($rand >= 0) { echo '<img src="image/upload/'.$imgarray[$rand].'" width="125" height="125">'; //replace path, width & height sizes to fit your needs } ?> Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 16, 2007 Share Posted March 16, 2007 I don't see any uploading happening... looks like that just displays a random image from the "image/upload" directory. Quote Link to comment Share on other sites More sharing options...
JayLewis Posted March 16, 2007 Author Share Posted March 16, 2007 My Bad i got the wrong bit of code.. sorry <?php $target = "image/upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 <?php $target = "image/upload/"; $target = $target . "newnameappend-". basename( $_FILES['uploaded']['name']) ; // This is where you designate a new name. $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
bwochinski Posted March 16, 2007 Share Posted March 16, 2007 ahh there ya go So here when you're using move_uploaded_file() that's where you're actually renaming the temporary file to (in your case) the filename in $target. You do not by any means have to use the name from $_FILES['uploaded']['name']. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 http://us3.php.net/manual/en/function.move-uploaded-file.php Explains it perfectly. And you are right by no means are u required to use the name stored in $_FILES. Quote Link to comment Share on other sites More sharing options...
JayLewis Posted March 16, 2007 Author Share Posted March 16, 2007 Ahhh, Thanks alot people much appreciated. Works a treat Nice one! Quote Link to comment Share on other sites More sharing options...
JayLewis Posted March 16, 2007 Author Share Posted March 16, 2007 would it be possible to change the name alltogether? newnameappend-picname.jpg is what i am getting... is there a way to make it like: newnameappend-001 Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 <?php $target = "image/upload/"; $target = $target . "newnameappend-001.jpg" ; // This is where you designate a new name. That and .jpg should match the uploaded extension. $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
JayLewis Posted March 16, 2007 Author Share Posted March 16, 2007 So i upload a pic its named: newnameappend-001 you upload one its called: newnameappend-002 right? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 You would need some way to increment to the next one, either a text file that stores the last value and you read it than increment it one than write it again or database or something. <?php $incrementVal = $oldVal++; // add one to old val note old val is not set in this script, you need to decide that. $target = "image/upload/"; $target = $target . "newnameappend-".$incrementVal.".jpg" ; // This is where you designate a new name. That and .jpg should match the uploaded extension. $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
JayLewis Posted March 16, 2007 Author Share Posted March 16, 2007 My php skills are very basic... how would i go about setting the "old Val"? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 16, 2007 Share Posted March 16, 2007 One way is using the fopen, fread, fwrite and fclose http://us3.php.net/manual/en/function.fopen.php <?php $oldVal = file_get_contents('nextval.txt'); $fp = fopen('nextval.txt', 'w'); fwrite($fp, ($oldval + 1)); fclose($fp); ?> 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.