roldahayes Posted December 2, 2008 Share Posted December 2, 2008 Hi there, I am using this very simple image upload script where a user can upload images to a floder on my server. Would it be possible to have a form on the page where a user enters their username and then all the images are renamed accordingly? i.e username_image1.jpg, username_image2.jpg etc. code is here <?php # The script is designed to allow you to upload multiple files in one go, the script also presents you with the variable option # of keeping a files name or randomly renaming it. # Remember, there maybe a a upload limit set by the server of 2MB, you can change this by changing the php.ini if you have access #************************************************************************************************** ###/ VARIABLES - CHANGE ACCORDINGLY define("VAR_BASE_DIRECTORY", "/var/www/vhosts/*******/subdomains/test/httpdocs/"); #/ Your webhosting directory define("VAR_UPLOAD_FOLDER", "uploads/"); #/ Chmod directory 777 for successful upload define("VAR_UPLOAD_DIRECTORY", VAR_BASE_DIRECTORY.VAR_UPLOAD_FOLDER); #/ DO NOT EDIT define("VAR_UPLOAD_FIELDS", 4); #/ Set number of upload fields define("VAR_FILENAME_KEEP", 1); #/ If set to 0 (Zero) the filename will generate randomly, if 1 (One) it will maintain filename ##/ Function that displays forms and is called by default function defaultForm() { echo "<form method=\"post\" enctype=\"multipart/form-data\">\n"; for($i=0; $i < VAR_UPLOAD_FIELDS; $i++) { echo "Upload field ".$i." <input name=\"file[]\" type=\"file\" id=\"file[]\" /><br />\n"; } echo "<input name=\"Submit\" type=\"submit\" value=\"Submit\">\n"; echo "<input name=\"filter\" type=\"hidden\" value=\"processForm\">\n"; ##/ hidden value points the switch to processing echo "</form>\n"; return; } #/ End of defaultForm ##/ Function that displays forms and is called by default function processForm() { for($i=0; $i < VAR_UPLOAD_FIELDS; $i++) { echo "Upload field $i "; if(!empty($_FILES[file][size][$i])) { if(VAR_FILENAME_KEEP==1) { ##/ File maintaining upload name $fileName = $_FILES[file][name][$i]; } else { ##/ Filename randomized $fileName = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3); } ##/ Creating reference address $newLocation = VAR_UPLOAD_DIRECTORY.$fileName; if(!copy($_FILES[file][tmp_name][$i],$newLocation)) { echo "<b>Failed - ".$_FILES[file][name][$i]." would not copy to ".$newLocation."</b> (Check your upload directory and permissions)"; } else { ###/ SUCCESS /### #/ Stripping of VAR_BASE_DIRECTORY for better viewing and linking $urlShow = str_replace(VAR_BASE_DIRECTORY,'',$newLocation); echo "<b>Uploaded successfully - <a href=\"$urlShow\" target=\"_blank\">$urlShow</a></b>"; } } else { echo "<b>No file uploaded</b>"; } echo "<br />"; } return; } #/ End of processForm ##/ This object handles which function the application should call switch($_POST[filter]) { case "processForm": processForm(); break; default: defaultForm(); break; } #/ End of Handling ?> Quote Link to comment Share on other sites More sharing options...
JD* Posted December 2, 2008 Share Posted December 2, 2008 Do you want to base this on a login scenario for the username, or just have a field that someone can type a name and then place that in front of the file name? In either case, you'll change this line: $fileName = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3); to $fileName = $username."_".rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3); Let me know what you're thinking and we can work through how you get to that username Quote Link to comment Share on other sites More sharing options...
roldahayes Posted December 2, 2008 Author Share Posted December 2, 2008 A username that they already have would be better. Basically, this upload page will only be visable to members who have already registered. Quote Link to comment Share on other sites More sharing options...
roldahayes Posted December 3, 2008 Author Share Posted December 3, 2008 on second thoughts, maybe just a text box for them to enter the user name before they select the files to upload - I just need a way to identify who has uploaded the images. Quote Link to comment Share on other sites More sharing options...
roldahayes Posted December 3, 2008 Author Share Posted December 3, 2008 Changed my mind again! I want to have the users login name appear before the image title. can i use something like $_SESSION['LOGIN_NAME']; ??? in the line; ##/ Filename randomized $fileName = rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3); } Instead of it being a random name? Quote Link to comment Share on other sites More sharing options...
JD* Posted December 6, 2008 Share Posted December 6, 2008 Absolutely. Whatever session is storing their username, just prepend it to the filename string like this: ##/ Filename randomized $fileName = $_SESSION['LOGIN_NAME'].rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).rand(1,4000).'.' . substr($_FILES[file][name][$i], -3); } 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.