monkeytooth Posted November 10, 2008 Share Posted November 10, 2008 I am setting up something so my users can upload files. Which with that I am able to do, however I want to rename the files after they are uploaded. Which is where I am stuck. Can anyone help me on this? I can get the existing file name using this: $_FILES['file']['name'] what I am doing is uploading to a community folder so to speak, and keeping track of who's file is who's via a database. I just want all the files to be in one unknown spot on the server with new names on the files so they can only be accessed via my scripts (for the most part).. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/ Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 move_uploaded_file() does this. Just figure out a good way to generate the name. Something like this works: $name = microtime(true) . getmypid(); Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687117 Share on other sites More sharing options...
monkeytooth Posted November 10, 2008 Author Share Posted November 10, 2008 This is what I am doing now.. if(is_uploaded_file($_FILES['file']['tmp_name'])) { move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); } print "Your file has been uploaded successfully! Yay!<br />" . $_FILES['file']['name'] . ""; } else { print "Incorrect file extension!"; } How would I flop that in? Sorry to sound off about things.. last time I did file uploads with php, the whole move_ thing wasnt around so im a bit off relearning as i go with this one... Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687138 Share on other sites More sharing options...
wildteen88 Posted November 10, 2008 Share Posted November 10, 2008 Use if(is_uploaded_file($_FILES['file']['tmp_name'])) { $new_filename = (microtime(true) . getmypid()) . $_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename); print "Your file has been uploaded successfully! Yay!<br />" . $new_filename; } else { print "Incorrect file extension!"; } Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687144 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 You don't need the if (is_upload_file()) bit; move_uploaded_file() checks for you. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687147 Share on other sites More sharing options...
monkeytooth Posted November 10, 2008 Author Share Posted November 10, 2008 ive swapped it, but now im getting an unexpected $end.. not sure from what all the brackets seem to be in place my guess something with the $new_filename = (microtime(true) . getmypid()) . $_FILES['file']['name']; but i dunno, something to me just seems off with that Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687162 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Nah, there's nothing wrong with that. Can I see the whole script, or that whole block at least? Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687163 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 $new_filename = microtime(true) . getmypid() . $_FILES['file']['name']; You didnt need those parans around microtime and getmypid. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687164 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 He didn't need it, but it wouldn't cause an error. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687166 Share on other sites More sharing options...
monkeytooth Posted November 10, 2008 Author Share Posted November 10, 2008 <?php $uploaddir = "fileupz"; $allowed_ext = "doc, pdf, png, jpg"; $max_size = "460800"; // 450kb $max_height = ""; //if intent on uploading images hxw.. $max_width = ""; $extension = pathinfo($_FILES['file']['name']); $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } if ($ok == "1") { if($_FILES['file']['size'] > $max_size) { print "File size is too big!"; exit; } elseif($_FILES['file']['size'] <= "0") { print "File size is too Small 0kb!"; exit; } else { } if ($max_width && $max_height) { list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']); if($width > $max_width || $height > $max_height) { print "File height and/or width are too big!"; exit; } } if(is_uploaded_file($_FILES['file']['tmp_name'])){ $$new_filename = microtime(true) . getmypid() . $_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename); print "Your file has been uploaded successfully! Yay!<br />" . $new_filename; } else { print "Incorrect file extension!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687183 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 First thing I noticed was 2 $$ in front of $$new_filename. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687188 Share on other sites More sharing options...
wildteen88 Posted November 10, 2008 Share Posted November 10, 2008 You do not have a closing brace for this if: if ($ok == "1") { However looking at your script. This portion of code $allowed_ext = "doc, pdf, png, jpg"; ... $extension = pathinfo($_FILES['file']['name']); $extension = $extension[extension]; $allowed_paths = explode(", ", $allowed_ext); for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; } } if ($ok == "1") { can be changed to $allowed_ext = array("doc", "pdf", "png", "jpg"); ... $img = pathinfo($_FILES['file']['name']); if (in_array($img['extension'], $allowed_ext)) Which is much cleaner. Everything put together: <?php $uploaddir = "fileupz"; $allowed_ext = array("doc", "pdf", "png", "jpg"); $max_size = "460800"; // 450kb $max_height = ""; //if intent on uploading images hxw.. $max_width = ""; $img = pathinfo($_FILES['file']['name']); if (in_array($img['extension'], $allowed_ext)) { if($_FILES['file']['size'] > $max_size) { print "File size is too big!"; exit; } elseif($_FILES['file']['size'] <= "0") { print "File size is too Small 0kb!"; exit; } if ($max_width && $max_height) { list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']); if($width > $max_width || $height > $max_height) { print "File height and/or width are too big!"; exit; } } if(is_uploaded_file($_FILES['file']['tmp_name'])) { $new_filename = microtime(true) . getmypid() . $_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$new_filename); print "Your file has been uploaded successfully! Yay!<br />" . $new_filename; } else { print "Incorrect file extension!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687196 Share on other sites More sharing options...
monkeytooth Posted November 10, 2008 Author Share Posted November 10, 2008 well that seems to have worked wonders, thanks for the help with that.. now all i gotta do is figure out to check to see if file exists (though I doubt the possibility exists unless i go a different route of rename.. may not though as i doubt there will ever be a chance in hell of the same file name ever with this concept.. then throw in the DB stuff to store what I want off of this, and ill be good I think.. Thanks again.. Quote Link to comment https://forums.phpfreaks.com/topic/132199-solved-renaming-a-file/#findComment-687208 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.