Jump to content

SaranacLake

Members
  • Posts

    648
  • Joined

  • Last visited

Posts posted by SaranacLake

  1. 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.

  2. 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!

     

  3. 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!

     

  4. 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...

     

     

  5. 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....

  6. 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.

     

     

     

  7. 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.

     

  8. 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?

     

  9. 1 hour ago, Barand said:

    You could've had a database set up in less time than you spent moaning about what a PITA it is.

    It takes about half a dozen statements to create an image table from your image directory.

    JFDI.

    
    const IMGDIR = 'images/';
    
    $db->exec("CREATE TABLE IF NOT EXISTS `image_lib` (
                  `img_id` int(11) NOT NULL AUTO_INCREMENT,
                  `name` varchar(100) DEFAULT NULL,
                  `path` varchar(255) DEFAULT NULL,
                  `width` int(11) DEFAULT NULL,
                  `height` int(11) DEFAULT NULL,
                  `mime_type` varchar(20) DEFAULT NULL,
                  `description` varchar(255) DEFAULT NULL,
                  `img_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                  PRIMARY KEY (`img_id`),
                  FULLTEXT KEY `idx_image_lib_description` (`description`)
                )
                ");
    
    $ins = $db->prepare("INSERT INTO image_lib (name, path, width, height, mime_type) VALUES (?,?,?,?,?)") ;
    $imgs = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE);
    foreach ($imgs as $i) {
        $s = getimagesize($i);
        $ins->execute( [
                        basename($i),
                        IMGDIR,
                        $s[0],
                        $s[1],
                        $s['mime']
                       ]);
    }

    Job done!

    Thanks for the code - might be handy if I go with a database solution.

     

    Um, @Barand, nothing is "easy" when you have been away from coding and database for 4-5 years (and are an old man) like me!!

     

    While you sample is useful - thanks - id does NOT cover all of the other things I need to do like creating a database in my DEV environment, writing code to read the DB and populate my PHP gallery, creating a PROD database on my server, migrating the DEV data to PROD, and don't forget data entry.

    If it was "easy", I wouldn't be griping here.

    Guess it will come down to how much time I have left - if any - at the end of this 4-day weekend.  (Can't believe I spent my whole holiday building something for work for free?!  Such is the addiction of a programmer with a problem to solve?!)  :happy-04:

  10. 9 hours ago, benanamen said:

    Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).

    !== does not just mean "not TRUE" - It means "not identical".

    Now where is the "lack of understanding"?  😉

    https://www.php.net/manual/en/function.readdir.php   (see the "Warning" section.)

    https://www.php.net/maunal/en/lanuage.operators.comparison.php

     

     

  11. Like many projects, this started small, and keeps growing!  :facepalm:

    I am building a photo gallery and would now like to add labels/captions below each photo, HOWEVER, I really don't want to have to build a database and do all the related coding.  (I know how, but its a PITA.)

    To display photos in the gallery, I read in files from my "images/" directory, store them in a simple array, and then iterate through the array to display the thumbnails in a gallery.

    I am wondering if there would be an easy way that I could merge photo metadata (e.g. a brief caption) with my array or something like that?

    If I could type up the file names and a brief description/caption in a Text File or Spreadsheet and then somehow slurp that into my code and merge it with my array or something like that, then I would be willing to type up captions.  (This is for like 600 photos which is why I don't want to do a database as data entry in phpMyAdmin is a PITA!)

    Any suggestions how I could do this and add a "cherry on the top" of this mini project I am doing for my co-workers?

    Thanks!

     

     

  12. 8 minutes ago, maxxd said:

    In the second code block, you're only running the readdir() once, so as long as it's not false on the initial run, it never will be.

    That's why you are a "guru"!!  😉

     

    Is there a cleaner way to write this...

    	while (($file = readdir($opendirectory)) !== false){
    	

     

    As you can see, I tried to break that up into two parts, because I hate the idea of doing an *assignment* and a *logical comparison* all in one statement.  Unfortunately I broke the logic as you pointed out.

  13. Why does this code seem to run okay...

    $directory = "images/";
    
    // Open a directory, and read its contents
    if (is_dir($directory)){
      if ($opendirectory = opendir($directory)){
        while (($file = readdir($opendirectory)) !== false){
          echo "filename:" . $file . "<br>";
        }
        closedir($opendirectory);
      }
    }
    	

     

    And this code sends my computer into an infinite loop...

    $directory = "images/";
    
    if (is_dir($directory)){
    	$opendirectory = opendir($directory);
        
        if($opendirectory){
        	$file = readdir($opendirectory);
            
            while($file !== FALSE){
            	echo $file . "<br>";
    		}
            
            closedir($opendirectory);
    	}
    }
    

     

     

  14. 40 minutes ago, Barand said:

    The link that @chhorn gave you has an example that shows exactly how it would help. All you had to do was click it and read.

    I did read it and at first read it just seemed like a regex.  I looked at the link again and it sorta makes sense, and am trying it now.

    I recall seeing this done in the past and you had to use all of these file commands which is why I was confused by his answer.

    Is glob supported by all versions of PHP?

    Are there other ways to do this?  (It sounds like it lacks leveraging regex, which probably doesn't matter in my immediate need, but still.

  15. I have a directory full of pictures that I would like to display in a gallery.

    Originally I was going to write HTML like this...

    	<li>
    	    <img src=/photos/img_001.jpg">
    	</li>
    	

    And then simply copy and paste that for the number of photos I have, and then tweak the code to: img_002.jpg, img_003.jpg, and so on.

    I guess that is silly considering that I have over 500 photos to display!

    How could I use PHP to go to my /photos/ directory, scan all of the files in that directory, and then grab the file name so I could automate this process?

    Sorry, but I haven't written PHP in several years so all of this escapes me!  ☹️

  16. 9 minutes ago, requinix said:

    You know, the regular user management system? You don't have to have open registration, but emails and passwords and all that.

    I could do that, but first of all, that would require that I have everyone's email addresses.  (I am going to send out an email from work to our work distribution list, but I suspect people will view this from home - if they are smart they won't access my website from work!)  And as far as passwords, if I want people to set their own password then you basically have a registration system.  I know how to do that, but don't want to spend that much time coding things.

    I take security very seriously, but it doesn't seem that bad to share a password with co-workers for temporary access.  Of course I wouldn't do that with a website like this or an e-commerce site, but it seems like a reasonable approach, or do you think I'm way off here?

    Also, since you recommended just using .htaccess, that would be the same issue of "lax" security, right?

     

×
×
  • 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.