Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile The sequence number for $filename

    Hi everyone, in the last few days, I tried to used Flash and PHP to make a program that can take photos with webcam and upload the photos into server folder.

    Inside my php code "$filename = "images1/visualstudio". mktime(). ".jpg";" is the reference I found online, I understand the "mktime()" was generating the combination of date and time,but what I really need is when the photos that upload to folder will be name with sequence number like: "visualstudio01.jpg","visualstudio02.jpg","visualstudio03.jpg"...

    Because this is the first time I deal with PHP, from my logic, the code should be "$filename = "images/visualstudio".SEQUENCE NUMBER CODE.".jpg";", but the problem is I don't know how the "SEQUENCE NUMBER CODE" structure should look like, can anyone give me some suggestion or some references?


    And here is my php code:
    ---------------------------------------------------------------------------------------
    [PHP CODE]
    ---------------------------------------------------------------------------------------
    <?php

    //Now get the encoded image form flash through HTTP_RAW_POST_DATA
    if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
    $img = $_GET["img"];

    //image Directory
    $filename = "images1/visualstudio". mktime(). ".jpg";
    file_put_contents($filename, $jpg);
    } else{
    //show error if image is not recived
    echo "Encoded JPEG information not received.";
    }
    ?>
    ---------------------------------------------------------------------------------------
    [/PHP CODE]
    ---------------------------------------------------------------------------------------

    Thanks for your time !!

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Let's assume that the filename is given in the input of a form. Let's say it's myphoto, and you want the filenames to go myphoto-0, myphoto-1, with whatever extension. You just use the glob function to find all the files in the folder with myphoto- as the filename, say myphoto-*.jpg, and take the count of that. And start naming the uploaded batch of files with that count, until they are all named.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    DrDOS,

    From my understanding from your reply, so what u mean are something like below?



    ------------------------------------------------
    PHP CODE
    ------------------------------------------------

    <?php

    //Now get the encoded image form flash through HTTP_RAW_POST_DATA
    if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
    $img = $_GET["img"];

    //image Directory
    $filename = "images1/visualstudio-*.jpg";
    file_put_contents($filename, $jpg);
    } else{
    //show error if image is not recived
    echo "Encoded JPEG information not received.";
    }
    ?>

    ------------------------------------------------
    /PHP CODE
    ------------------------------------------------



    Sorry if any stupidity question, since I'm noob in php

  • #4
    New Coder
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post
    change your code by the following one:

    $date = date('Y-m-d-g-i-s');
    $rand= rand(0, 1000);
    $image_nameC=$rand.$date.'.'.$extension;

    Hope thisn helps.

  • #5
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    priyankagound,

    I tried the code, but that one are the random number code, what I want are the sequence number, like 01,02,03,04...

  • #6
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Maybe something like this would work for what you want,
    PHP Code:
    <?php
    //Now get the encoded image form flash through HTTP_RAW_POST_DATA
    if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $jpg $GLOBALS["HTTP_RAW_POST_DATA"];
    $img $_GET["img"];

    $searchTerm "visualstudio"// filename without the extension or any numbers

    // Define the full path to your folder from root
    $path "/home/user/public_html/files";

        
    // Open the folder
        
    $dir_count = @opendir($path) or die("Unable to open $path");
        
        
    // set the counter to 0
        
    $anyResults 0;
        
        
    // Loop through the files
        
    while (false !== ($num readdir($dir_count))) {
            if(
    $num == "." || $num == ".." || $num == "index.php") {
                if (
    preg_match("/".$searchTerm."/i"$num)) {
                    
    $anyResults++; // increase counter on match    
                
    }
            }
        }
        
        
    closedir($dir_count);
        
        
    $filename "visualstudio".$anyResults.".jpg"// add counter to filename
        
    file_put_contents($filename$jpg);    
    } else{
        
    //show error if image is not recived 
        
    echo "Encoded JPEG information not received.";
    }
    ?>
    This should open the directory you specify and will match filenames with the filename you chose. It will count each match and then you could just apply the number of matches to the filename. I haven't tested the code but it should work as I have used it before.

    I'm sure there is possibly an easier way to do this though.

    EDIT: haha ofcourse glob like DrDos said. I should read things properly :/
    Last edited by jswany; 10-17-2013 at 12:13 PM.
    I'd love to change the world, but they wont give me the source code.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Generating the sequence number is the same as in any programing language, you increment the last number used. $sn++ will add 1 to the number $sn. However you may need to remember the last number used, in case the server gets turned off. So just use file_get_contents to get the last number from a file stored on the server, increment that value, then use file_put_contents to update to the next number. You can also use the glob method I mentioned earlier to get the actual count of the images stored, and make the new image name with that count..
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,760
    Thanks
    23
    Thanked 546 Times in 545 Posts
    I just love glob(). Thank you for introducing me to it DrDos. Now, if I can only remember to use it.

    Anyway, it becomes an easy task to do this
    in case the server gets turned off
    something I have never prepared for. So if $sn is the sequence number last used:
    PHP Code:
    <?php
    $sn 
    count(glob("*.jpg"));
    ?>
    and $sn++ is what you use on the image you just uploaded. I hope I remember to use this next time I need to do this....
    Evolution - The non-random survival of random variants.


  •  

    Tags for this Thread

    Posting Permissions

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