I've gotten the file to upload, but I want it to be able to randomize the file name instead of the file's actual extension name. Like instead of /uploads/image.png it's /uploads/73d92cgj_dwsd.png I'm using: <?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { 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."; } } ?> PHP:
I did a little research, and I found a couple of websites that might help you with randomizing names: http://php.about.com/od/advancedphp/ss/rename_upload_3.htm http://www.webmasterworld.com/forum88/8039.htm
I usually put a timestamp on the end of my filenames. You could do something like the following; // set the target folder for the image $target = $_SERVER['DOCUMENT_ROOT'] . "/files/"; // add a timestamp to the image name $timestamp = date('U'); $file_arr = explode(".",$_FILES['file']['name']); $file = $timestamp . "." . $file_arr[1]; $target = $target . $file; if(!(move_uploaded_file($_FILES['attachments']['tmp_name'], $target))) PHP:
@Weirfire - Yours would work as long as the user didnt have a . in their file name for instance say if they had it like "test.test2.jpg" it would return test2. @Jake-Johnson: Here you go buddy... Didn't get to test it but should be good to go .. Leave rep if you use. <?php function createRandomString() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $string = ''; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $string = $string . $tmp; $i++; } return $string; } $newimgname = createRandomString(); $target = "upload/"; $ext = strrchr(basename( $_FILES['uploaded']['name']), '.'); $target = $target.$newimgname.$ext; $ok=1; //This is our size condition if ($uploaded_size > 350000) { echo "Your file is too large.<br>"; $ok=0; } //This is our limit file type condition if ($uploaded_type =="text/php") { echo "No PHP files<br>"; $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0) { Echo "Sorry your file was not uploaded"; } //If everything is ok we try to upload it else { 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."; } } ?> PHP:
true - you could change the line $file = $timestamp . "." . $file_arr[1]; PHP: to $file = $timestamp . "." . $file_arr[count($file_arr)-1]; PHP: