SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    count() files: why does it count 1,1,2 ...?

    Hi,
    this was a general question: I don't know if what I did is wrong but I'm confused.
    I am counting files in a directory so that people can't upload more than x number of files without deleting some.
    What I have noticed is that the first time I upload a file '$count' is given as 1 (echoed).
    The second time is also 1, then 2 etc.

    I realised that I was counting the files BEFORE saving the new upload to the directory in question, so that explained why my echoes always were 1 behind the real value, but I cannot figure out why it says '1' the FIRST time, when the directory is empty.

    If it was counting a 'hidden' file or something, wouldn't it carry on from there: 1,2,3.... Likewise, if it's counting the uploaded (but not saved) file?

    My server is running on Linux, if that makes a difference.

    I was thinking this might be a general knowledge type thing rather than the code. I haven't tried working with files before and there seems to be a lot I don't know.

    here is the relevant part of my script if it helps ...


    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {

    $filename = basename($_FILES['uploaded_file']['name']);
    $ext = substr($filename, strrpos($filename, '.') + 1);
    if ((($ext == "jpg") || ($ext == "gif") || ($ext == "png")) && (($_FILES["uploaded_file"]["type"] == "image/jpeg") || ($_FILES["uploaded_file"]["type"] == "image/gif") || ($_FILES["uploaded_file"]["type"] == "image/png")) &&
    ($_FILES["uploaded_file"]["size"] < 30000)) {

    $newname = dirname(__FILE__).'/pictures/uploads/'.$venue.'/'.$filename;

    if (!file_exists($newname)) {

    $directory = 'pictures/uploads/'.$venue.'/';


    $filecount = count(glob("" . $directory . "*"));

    echo $filecount; ////THIS IS THE PART THAT ECHOES 1 TWICE


    if ($filecount <= 11) {

    if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
    $new_query = "UPDATE venues SET Pic = '$filename' WHERE VenID = '$venue' LIMIT 1;";
    $new_sql = mysql_query($new_query,$dbh);
    if (!$new_sql) {
    echo "<br />oops! can't change photo because - <br>";
    die('Could not query:' . mysql_error());
    exit;
    }
    else {
    echo "<br />Your venue's photo has been changed.<br />";
    }
    echo "<br />The photo has been saved in your portfolio<br />";
    }
    else {
    echo "<br />Error: A problem occurred during file upload!<br />";
    }
    }
    else {
    echo '<br />Error: there are already 12 photos in your portfolio. You can delete photos via View Portfolio.<br />';
    }
    }
    else {
    echo "<br />Error: File ".$_FILES["uploaded_file"]["name"]." already exists<br />";
    }
    }
    else {
    echo "<br />Error: Only .jpg .gif or .png images under 30KB are accepted for upload<br />";
    }
    }
    else {
    echo "<br />No file uploaded<br />";
    }

  2. #2
    SitePoint Addict CVPer's Avatar
    Join Date
    Sep 2007
    Location
    Vancouver, BC, Canada
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess the function glob also finds the directory itself and all files under it. So, when the directory is empty, glob() still return an array with one element.

    Steve
    * @location Vancouver, BC, Canada
    * @name Steve
    * @job PHP/MySQL, Drupal, WordPress Developer

  3. #3
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm yes that was what I thought but I don't understand why it doesn't continue to count from there. I couldn't find an example on the internet, although I found plenty of people whose count was 2 more than it should be (they needed to tell it to ignore '.' and '..' : the current directory and the parent), but nothing exactly like this. Some posts pointed to different Linux installations having their quirks, so I guess maybe this is one (Red Hat)?

    Anyway, it works for my purposes and I may try explicitly setting $filecount to zero at the beginning and see if that helps. I *think* I already tried telling it to ignore '.' and '..' but it was very late so I may have got confused.

    Edit: I remember I had an inspiration last night as I was getting to sleep. Is it that count() returns TRUE even if $count = 0, and somehow the TRUE is being echoed as 1? Would declaring $filecount as an int help here? Possibly not as PHP tends to change types whenever it feels like it but I could try to test for this with a conditional and echo '0' (?). I'll know if I manage to get 0,1,2 ...
    I had this problem in another part of the site, where a zero (int) was being interpreted as FALSE by one of PHP's inbuilt functions! Took me a while to spot the problem there too!

    Thanks for trying to help, Steve!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Can you write a quick test script like the following and tell us what happens?
    PHP Code:
    <?php
    // change somefolder to a folder that exists but is empty
    $dir   'pictures/uploads/somefolder/';
    $files glob($dir.'*'GLOB_ERR);
    var_dump($files);
    ?>
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Can you write a quick test script like the following and tell us what happens?
    PHP Code:
    <?php
    // change somefolder to a folder that exists but is empty
    $dir   'pictures/uploads/somefolder/';
    $files glob($dir.'*'GLOB_ERR);
    var_dump($files);
    ?>
    Hi, I tried this on a specially-created empty folder and got
    bool(false)

    so there we have it! If I test whether the folder is empty first, I won't have a problem. My script already does this elsewhere, so that shouldn't be hard.

    As I said, it works for my purposes, but it would be nice to have a message after uploads that told them how many photos they have, so if I can make this completely accurate that will be a good thing ...

    I also hate not understanding why things go wrong! I could have saved myself 2 hours otherwise!

    Thanks so much for the help

  6. #6
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SOLVED!!!! (I hope)

    I tried this and it returns 1:

    <?php
    $array = array();
    $array[] = NULL;
    $count = count($array);
    echo $count;
    ?>
    this also returns 1 if $array[] = FALSE
    without $array[] = NULL it returns zero.
    This all seems as it should be ...
    If I set $array = FALSE in the above example I also get 1.

    php.net says of count() : "If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned."

    This is misleading in this case, at least. I still need a bit more thinking to figure out exactly what is going on here, but this is obviously the key. PHP.net says glob "Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.", so it *should* be returning an empty array but is returning FALSE (or an array of one with value NULL).

    I must admit I am confused, but nearly there!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •