TechnoDiver Posted November 8, 2021 Share Posted November 8, 2021 Hi, Freaks I have this function -> <?php public function assignDefaultProfilePic() { $path = "img/profile_pics/defaults/"; $files = glob($path . '*.png'); $file = array_rand($files); return $files[$file]; // print_r($files); } and it's the weirdest thing. If I uncomment the print_r() and leave only $path and $files it prints the array of directory contents and everything seems fine. When I use it as is I get the following error-> Quote Fatal error: Uncaught ValueError: array_rand(): Argument #1 ($array) cannot be empty..... I don't understand how it's possible, I've changed nothing else. Can someone please explain this to me?! Quote Link to comment https://forums.phpfreaks.com/topic/314184-glob/ Share on other sites More sharing options...
TechnoDiver Posted November 8, 2021 Author Share Posted November 8, 2021 (edited) I couldn't edit my last post, thankfully I copied it before clicking the button: EDIT: Ok, so I was testing some things. This is in the registration form a I've edited my code slightly. And I'm trying to get it to assign the path string to the DB upon registration, but it keeps coming back as empty array when the 'register' button is clicked. which means it's happening at the array_rand() function, but if I sign in as a registered user and echo it out it prints the string. I don't understand why it's doing this. here is my edited code -> <?php public function assignDefaultProfilePic() { $path = "img/profile_pics/defaults/"; $files = glob($path . '*.png'); $file = array_rand($files); $f = explode('profile_pics/', $files[$file]); $profile_pic = end($f); return $profile_pic; // echo $profile_pic; } and here is the code at the top of my registration page about halfway down in the validation->passed if statement is where I'm building the registration array where I need to profile_pic info-> <?php require_once($_SERVER["DOCUMENT_ROOT"] . "/qcic/assets/core/init.php"); $html = ""; $errors = array(); $sent = false; $js_switch = true; // var_dump(Token::check(Input::get('token'))); if(Input::exists()) { if(Input::get("register_button")) { if(Token::check(Input::get('token'))) { $validate = new Validate(); //create requirements $validation = $validate->check($_POST, array( "username" => array( "name" => "Username", "required" => true, "min" => 2, "max" => 20, "unique" => "users" //unique to the users table ), "email" => array( "name" => "Email", "required" => true, //have to validate email, think I'll make it a public static function //"valid_email" => false ), "email2" => array( "name" => "Confirmation email", "required" => true, "matches" => "email" ), "password" => array( "name" => "Password", "required" => true, "min" => 6, ), "password2" => array( "name" => "Confirmation password", "required" => true, "matches" => "password" ) )); if($validation->passed()) { $user = new User(); // $profile_pic = $user->assignDefaultProfilePic(); try { $user->create(array( "username" => Input::get("username"), "password" => Hash::make(Input::get("password")), "email" => Input::get("email"), // default profile pic functionality goes here // "profile_pic" => $profile_pic, "profile_pic" => $user->assignDefaultProfilePic(), "group" => 2, )); $sent = true; $js_switch = true; } catch(Exception $e) { die($e->getMessage()); } } else { $errors = $validation->errors(); } } } elseif(Input::get("login_button")) { require("login.php"); } } ?> Any insight on this would be really appreciated Edited November 9, 2021 by TechnoDiver Quote Link to comment https://forums.phpfreaks.com/topic/314184-glob/#findComment-1591863 Share on other sites More sharing options...
TechnoDiver Posted November 9, 2021 Author Share Posted November 9, 2021 Another edit: EDIT: I've tried to do this statically and assign $profile_pic at the top of the registration.php page and I get the empty array error still. I've also taken it out of the User class and placed it as a function at the top of the registration.php page and I'm still getting the empty array error. -> <?php require_once($_SERVER["DOCUMENT_ROOT"] . "/qcic/assets/core/init.php"); $html = ""; $errors = array(); $sent = false; $js_switch = true; function assignDefaultProfilePic() { $path = "img/profile_pics/defaults/"; $files = glob($path . '*.png'); $file = array_rand($files); $f = explode('profile_pics/', $files[$file]); $profile_pic = end($f); return $profile_pic; // echo $profile_pic; } // $profile_pic = User::assignDefaultProfilePic(); and -> <?php if($validation->passed()) { $user = new User(); // $profile_pic = $user->assignDefaultProfilePic(); try { $user->create(array( "username" => Input::get("username"), "password" => Hash::make(Input::get("password")), "email" => Input::get("email"), // default profile pic functionality goes here // "profile_pic" => $profile_pic, "profile_pic" => assignDefaultProfilePic(), // "profile_pic" => $profile_pic, "group" => 2, )); I don't understand why it only works (gets the array) while logged in as an already registered user. I can't even think of anything else to try. Looking forward to your responses Quote Link to comment https://forums.phpfreaks.com/topic/314184-glob/#findComment-1591864 Share on other sites More sharing options...
requinix Posted November 9, 2021 Share Posted November 9, 2021 The most obvious thing I can see is that you're using a relative path for the images. Don't. Quote Link to comment https://forums.phpfreaks.com/topic/314184-glob/#findComment-1591867 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.