Jump to content

glob()


TechnoDiver

Recommended Posts

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?!

Link to comment
Share on other sites

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 by TechnoDiver
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.