coupe-r Posted June 6, 2009 Share Posted June 6, 2009 Hey everyone. My script works fine, I would like to know how to rename the file once it is uploaded. For instance, if they upload green.jpg, I was to rename the file to their company name ($_SESSION['company']) . JPG. if(is_uploaded_file($_FILES['file']['tmp_name'])) { $ext = strtolower(substr(($t=strrchr($_FILES['file']['name'],"."))!=false?$t:'',1)); if(!in_array($ext, $filetype)) { $upload_error = '<FONT color="red"><b>Error:</b></FONT>' . ' ' . "Allowed filetypes: $ftype"; } elseif($_FILES['file']['size'] > $max_size*1024) { $upload_error = '<FONT color="red"><b>Error:</b></FONT>' . ' ' . "File is too big. Max file size 1 MB."; } else { $fn = substr($_FILES['file']['name'], 0, strrpos($_FILES['file']['name'], '.')); $f = $fn.".".$ext; if(move_uploaded_file($_FILES['file']['tmp_name'], $folder.$f)) { mysql_query("INSERT INTO uploads VALUES(NULL, '/support/admin/uploads/".$_FILES['file']['name']."', '".$_SESSION['admin_id']."', '".$_SESSION['company']."', '".$_FILES['file']['name']."')"); $uploaded = true; chmod($folder.$f, 0777); } Link to comment https://forums.phpfreaks.com/topic/161153-solved-once-file-is-uploaded-rename-file/ Share on other sites More sharing options...
trq Posted June 6, 2009 Share Posted June 6, 2009 So, rename it when you call move_uploaded_file. Link to comment https://forums.phpfreaks.com/topic/161153-solved-once-file-is-uploaded-rename-file/#findComment-850410 Share on other sites More sharing options...
coupe-r Posted June 6, 2009 Author Share Posted June 6, 2009 Unfortunately, I am not sure how to make it a session variable with a " . EXT " Link to comment https://forums.phpfreaks.com/topic/161153-solved-once-file-is-uploaded-rename-file/#findComment-850411 Share on other sites More sharing options...
trq Posted June 6, 2009 Share Posted June 6, 2009 A simple concatination (joining strings) example. $ext = 'jpg'; $_SESSION['foo'] = 'blah'; $filename = $_SESSION['foo'] . '.' . $ext; Link to comment https://forums.phpfreaks.com/topic/161153-solved-once-file-is-uploaded-rename-file/#findComment-850412 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.