August 3, 201115 yr I am really struggling to firgure out how to create an image upload script that uploads an image an gives it a random name say the file was "image.jpg" - It would change it to something like "938765.jpg". Can someone please edit my upload script so this happens and if possible explain what it is that makes it happen, thank you. <? $uploaddir = "companylogo"; $allowed_ext = "jpg, jpeg, png, png, bmp, gif"; $max_size = "10000"; $max_height = "100"; $max_width = "30"; $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; } if(is_uploaded_file($_FILES['file']['tmp_name'])) { move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); } echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, <a href="' .$uploaddir.'/'.$_FILES['file']['name'] .'">Click here to view.</a></div></div>'; } else { print 'Please select a valid file.'; } ?>
August 3, 201115 yr When I want to create a completely random string I always use a current timestamp as an encryption key. This means that the string can never be repeated, in short: $random = md5(time().uniqid()); Your script: <? $uploaddir = "companylogo"; $allowed_ext = "jpg, jpeg, png, png, bmp, gif"; $max_size = "10000"; $max_height = "100"; $max_width = "30"; $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; } if(is_uploaded_file($_FILES['file']['tmp_name'])) { /// we'd need to get the extension $ext = pathinfo($_FILES['file']['name']); $ext = $ext['extension']; $random_filename = md5(time().uniqid()); move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$random_filename.".".$ext); } echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, <a href="' .$uploaddir.'/'.$_FILES['file']['name'] .'">Click here to view.</a></div></div>'; } else { print 'Please select a valid file.'; } ?>
August 3, 201115 yr Author Yeeah I have, the 'click here to view' didn't have the randomised link in so I did this: echo '<div align="left"><div id="indent">Your logo has been uploaded successfully, <a href="' .$uploaddir.'/' . $random_filename . '.' . $ext . '">Click here to view.</a></div></div>'; Works perfect!
Create an account or sign in to comment