Jump to content

Recommended Posts

Is there a limit to how many items can be stored in an array?

My code looks in a directory of photos and writes the file names into the one-dimensional array.

I just switched from my test folder to my actual photo folder - about 600 photos - and when I ran my script with var_dump enabled, my array only goes up to 128 items (0-127).

I sure hope arrays aren't that wimpy?!

Each photo is about 1-2MB in size, but since I am only storing the file name, I would expect that I could easily store thousands of elements in this array.

What is going on?

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/
Share on other sites

35 minutes ago, Barand said:

Must be something you are doing. Post code for the benefit of the clairvoyantly challenged.

 

Here is my code...

	<?php
	  $path = "../WORKSPACE/images/";
  $files = array();
  
  // Check for Directory.
  if (is_dir($path)){
    // Open Directory-Handle.
    $handle = opendir($path);
	    if($handle){
      // Iterate through Directory items.
      // Return name of next item in Directory (i.e. File or Folder).
      while(($file = readdir($handle)) !== FALSE){                
        if($file != '.' && $file != '..' && preg_match("#^[^\.].*$#", $file)){
          // Not Directory, Not Hidden File
          // Add to array.
          $files[] = $file;                     // Simple array.
//          $files[] = array($file);              // Nested array.
        }
      }
      closedir($handle);
    }
  }
	  var_dump($files);
  exit();
	?>
	

 

By the way, thanks for trying to help last night, but I couldn't really follow your glob( ) example so I took the above approach which was easier for me.

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572031
Share on other sites

Running your readdir() code on one of my folders, with 528 files, loads all of them into the array.

Last bit of my var_dump...

  [524]=>
  string(9) "xmlns.php"
  [525]=>
  string(15) "xmltestdata.xml"
  [526]=>
  string(11) "YMDdiff.php"
  [527]=>
  string(10) "zodiac.txt"
}

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572036
Share on other sites

4 minutes ago, Barand said:

Running your readdir() code on one of my folders, with 528 files, loads all of them into the array.

Last bit of my var_dump...


  [524]=>
  string(9) "xmlns.php"
  [525]=>
  string(15) "xmltestdata.xml"
  [526]=>
  string(11) "YMDdiff.php"
  [527]=>
  string(10) "zodiac.txt"
}

 

 

So what could the issue be?

Does it have to do anything with the filesize?

I have like 600 1-2MB photos in the directory.

Am using my Mac for dev work.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572037
Share on other sites

7 minutes ago, SaranacLake said:

Does it have to do anything with the filesize?

I don't see anything in your code that has to do with file sizes.

7 minutes ago, SaranacLake said:

I have like 600 1-2MB photos in the directory.

Are they all in the same directory? Using any subdirectories?

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572038
Share on other sites

16 minutes ago, requinix said:

I don't see anything in your code that has to do with file sizes.

Are they all in the same directory? Using any subdirectories?

I have one sub-directory in the directory I point too, but it doesn't have that many photos in it.

The fact that my array went to 127 makes me thing it is something predicatable since that is 128 elements in the array.  (2^7=128)

I dunno....

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572041
Share on other sites

I also can't understand  why your array index values are limited to a single signed byte. I'd expect a maximum of at least 2 billion (memory permitting)

Array
(
    [2147483647] => 1
)

or even this on a 64 bit system

Array
(
    [9223372036854775807] => 1
)

 

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572042
Share on other sites

15 minutes ago, Barand said:

I also can't understand  why your array index values are limited to a single signed byte. I'd expect a maximum of at least 2 billion (memory permitting)

 

 

I think there are a couple of strange things going on here...

- When I uncomment Var_dump() and exit(), I do see the array count is correct (e.g. 1,210) but the actual displayed array is stopping at 128 elements and then says "more elements..." at the bottom.

- When I comment out var_dump() and exit() and run my web page, I see blanks for what appers to be 1,210 files, but I am only seeing photos for maybe 100 or so.  (Maybe 128?)

- I have movies from my phone intertwined with the JPG's.  I can try deleting those out of my "images/" folder, but I suspect this is a browser or memory ssue since 1,210 X 1-2 MB is A LOT of memory usage!!

- I need to learn how to make smaller thumbnails...

 

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572044
Share on other sites

I just sorted my original photos/videos by type, and grabbed all of the photos (i.e. 600) and pasted them in my websites "images/" directory.

When I re-ran things, it took a while, but it appears that all of the photos rendered on my web page.

That is nearly 1 GB of photos!!

I think my browser was choking on the extra video files and the sheer size of things?

Of course if I create thumbnails that will make it much easier to load things!

Still curious why var_dump( ) won't show more than 128 elements in the display even though it gives a proper count.

All very interesting!

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572046
Share on other sites

If you want to show a gallery then, yes, you really want thumbnail size images to display. You can have an image that is originally 4000 pixels x 2500 pixels and shrink it in the html image tag by specifying width='200' height = '125' so it looks like a thumbnail but you are still transferring the multi MB file. You therefore need to run a job to create thumbnail versions of all your images. (The best time to create them is when the image is uploaded but too late for that now).

Perhaps have an images folder with the original images and a thumbs folder to contain the thumbnail images, one for each original)

When you create the thumb images, decide on the size of square into which they should fit (say 150 or 200 pixels). Then calculate the thumbsize so the longest dimension just fits that square (be it landscape or portrait) then calculate what the shorter dimension needs to be to maintain the same aspect ratio. Then you just copy/resize the image to the new file.

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572048
Share on other sites

Note to self: next time someone talks about var_dump output, ask them to post said output. Because that way it will be much easier to spot when...

54 minutes ago, SaranacLake said:

- When I uncomment Var_dump() and exit(), I do see the array count is correct (e.g. 1,210) but the actual displayed array is stopping at 128 elements and then says "more elements..." at the bottom.

27 minutes ago, SaranacLake said:

Still curious why var_dump( ) won't show more than 128 elements in the display even though it gives a proper count.

You are using Xdebug.

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572049
Share on other sites

15 minutes ago, Barand said:

If you want to show a gallery then, yes, you really want thumbnail size images to display. You can have an image that is originally 4000 pixels x 2500 pixels and shrink it in the html image tag by specifying width='200' height = '125' so it looks like a thumbnail but you are still transferring the multi MB file. You therefore need to run a job to create thumbnail versions of all your images. (The best time to create them is when the image is uploaded but too late for that now).

Perhaps have an images folder with the original images and a thumbs folder to contain the thumbnail images, one for each original)

When you create the thumb images, decide on the size of square into which they should fit (say 150 or 200 pixels). Then calculate the thumbsize so the longest dimension just fits that square (be it landscape or portrait) then calculate what the shorter dimension needs to be to maintain the same aspect ratio. Then you just copy/resize the image to the new file.

Yeah, that is on to-do list putting this site together!

 

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572050
Share on other sites

7 minutes ago, requinix said:

Note to self: next time someone talks about var_dump output, ask them to post said output. Because that way it will be much easier to spot when...

You are using Xdebug.

Yeah, sorry, I didn't notice that the count was correct until trying several tests and then my eyes finally noticed that as I was searching for the issue.

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572051
Share on other sites

8 minutes ago, requinix said:

Note to self: next time someone talks about var_dump output, ask them to post said output. Because that way it will be much easier to spot when...

You are using Xdebug.

Yes, I do happen to have Xdebug set up in NetBeans, but I haven't been using it for this particular issue..

Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572052
Share on other sites

5 minutes ago, SaranacLake said:

How did you know I use Xdebug?

Did you notice the link in my post? The one that goes to a page whose very first sentence is "Xdebug replaces PHP's var_dump() function for displaying variables"?

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572056
Share on other sites

12 hours ago, SaranacLake said:

Yeah, that is on to-do list putting this site together!

Christmas has come early!

<?php
const IMGDIR = 'images/';
const THUMBDIR = 'thumbs/';
const THUMBSIZE = 150;                                        // max thumbnail dimension
const NUM = 100;                                              // number of images to be processed on each run

$images = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE);
$thumbs = glob(THUMBDIR.'{*.png,*.jpg}', GLOB_BRACE);
// reduce to basenames only
$images = array_map('basename', $images);
$thumbs = array_map('basename', $thumbs);

// copy the next NUM images to $todo list where thumbnails do not yet exist
$todo = array_slice(array_diff($images, $thumbs), 0, NUM);

$output = '';
foreach ($todo as $fn) {
    $sz = getimagesize(IMGDIR.$fn);
    if ($sz[0] == 0) continue;                                // not an image
    $ok = 0;
    $out = null;
    switch ($sz['mime']) {                                    // check the mime types
        case 'image/jpeg':
            $im = imagecreatefromjpeg(IMGDIR.$fn);
            $ok = $im;
            $out = 'imagejpeg';
            break;
        case 'image/png':
            $im = imagecreatefrompng(IMGDIR.$fn);
            $ok = $im;
            $out = 'imagepng';
            break;
        default:
            $ok = 0;
    }
    if (!$ok) continue;                                       // not png or jpg
    
    // calculate thumbnail dimensions
    if ($sz[0] >= $sz[1]) {                                   // landscape
        $w = THUMBSIZE;
        $h = THUMBSIZE *  $sz[1]/$sz[0];
    } else {                                                  // portrait
        $h = THUMBSIZE;
        $w = THUMBSIZE * $sz[0]/$sz[1];
    }
    // copy and resize the image
    $tim = imagecreatetruecolor(THUMBSIZE, THUMBSIZE);
    $bg = imagecolorallocatealpha($tim,0xFF,0xFF,0xFF,127);
    imagefill($tim, 0, 0, $bg);
    imagecolortransparent($tim, $bg);
    // centre the image in the 150 pixel square
    $dx = (THUMBSIZE - $w) / 2;
    $dy = (THUMBSIZE - $h) / 2;
    imagecopyresized($tim, $im, $dx, $dy, 0, 0, $w, $h, $sz[0], $sz[1]);
    imagesavealpha($tim, true);
    
    $out($tim, THUMBDIR.$fn);
    imagedestroy($im);
    imagedestroy($tim);
    $output .= "<img src='".THUMBDIR."$fn' alt='$fn'>\n";
}

?>
<!DOCTYPE html>                   
<html>
<head>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Create Thumbnails</title>
<meta name="author" content="Barry Andrew">
<meta name="creation-date" content="10/09/2019">
<style type="text/css">
    body   { font-family: verdana, sans-serif; font-size: 11pt; }
    header { background-color: black; color: white; padding: 15px 10px;}
    img    { margin: 5px; }
 </style>
</head>
<body>
    <header>
        <h1>New Thumbnail Images</h1>
    </header>
    <?=$output?>
</body>
</html>

 

  • Like 2
Link to comment
https://forums.phpfreaks.com/topic/309602-limit-to-php-array/#findComment-1572060
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.