]> jfr.im git - uguu.git/blobdiff - core.php
Update README.md
[uguu.git] / core.php
index 2e701762f687c9734d0c2a3dd1087931646195cc..3be8288e922bb38290a32ce4dd7a5db0f7e32e34 100644 (file)
--- a/core.php
+++ b/core.php
@@ -1,10 +1,42 @@
 <?php
-function save_file ($file, $name){
-    $rand_string = crc32(microtime(true).mt_rand(1000, 9000));
+function save_file ($file, $name, $arg){
+    //Where to save
     $path='/home/neku/www/files/';
-    $file_data=strip_tags($file);
-    $file_name=$rand_string.'_'.$name;
-    move_uploaded_file($file_data,$path.$file_name);
+    //Generate name depending on arg
+    switch($arg){
+        case 'random':
+            $ext = pathinfo($file.$name, PATHINFO_EXTENSION);
+            $file_name = gen_name('random', $ext);
+            while(file_exists($path.$file_name)){
+                $file_name = gen_name('random', $ext);
+            }
+            break;
+        case 'custom_original':
+            $name = strip_tags(preg_replace('/\s+/', '', $name));
+            $file_name = gen_name('custom_original', $name);
+            while(file_exists($path.$file_name)){
+                $file_name = gen_name('custom_original', $name);
+            }
+            break;
+    }
+    //Move the file to the above location with said filename
+    move_uploaded_file($file,$path.$file_name);
+    //Return url+filename to the user
     echo 'http://a.uguu.se/'.$file_name;
 }
+function gen_name($arg, $in){
+    $chars = 'abcdefghijklmnopqrstuvwxyz';
+    $name = '';
+    for ($i = 0; $i < 6; $i++) {
+    $name .= $chars[mt_rand(0, 25)];
+        }
+    switch($arg){
+        case 'random':
+            return $name.'.'.$in;
+            break;
+        case 'custom_original':
+            return $name.'_'.$in;
+            break;
+    }
+}
 ?>