#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72

    Function to throw up an alert when file/s are added to a folder


    Is it possible to make a function in php(preferably) or javascript, which will alert me, when file is added to a particular folder (OS - windows )?
    If yes, can some one provide some links or guidance ?

    Thanks
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,247
    Rep Power
    9400
    PHP because it can do generic scripting, yes. Javascript no.

    Alert how? How often does it need to check for the new file? A particular new file or anything new at all? Any other modifications to watch for?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Originally Posted by requinix
    PHP because it can do generic scripting, yes. Javascript no.
    If possible, request you to elaborate on php - generic scripting ( a couple of lines); javascript - not generic scripting.
    Tried googling, but couldn't find something easy to understand.

    Originally Posted by requinix
    Alert how? How often does it need to check for the new file? A particular new file or anything new at all? Any other modifications to watch for?
    Sorry for not having made things much clearer.
    The situation :
    I have a folder on my desktop, to which files (usually RTF or Word docs) are added, at any given time, during office hours. There is no definite time interval & this event can occur at any time.
    The moment a file is added, some comment has to be added by me on the file (This part is not relevant to the function).
    Currently, I have my colleagues, communicating with me, whenever they add files to the folder.
    Being always on the lookout for small automation & towards eliminating physical communication,
    am looking for a function, which will alert me immediately, whenever, a new file is added to the folder on my Desktop.

    Thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    875
    Rep Power
    276
    Since its for Windows, why not just use THIS ?

    Or THIS ONE looks even better. [download link is on THISPAGE.]
    Last edited by DonR; August 7th, 2013 at 02:34 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Originally Posted by DonR
    Since its for Windows, why not just use THIS ?

    Or THIS ONE looks even better. [download link is on THISPAGE.]
    Hi,
    Thanks for the links.
    Actually, Currently, I already have a tool "FolderActions" to do the work and was looking for a PHP script ( kind of learn while you use, to replace it).
    The 1st Link provided by you, "Watch-4-folder - Free version" ( I tried it ), is very appealing, even though, the free version has only 1 Folder monitoring.
    The 2nd link, I have not tried, though, from the description that I read, it also appears to be very useful, perhaps, even more.

    Thanks a lot.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Have currently come up with below :

    PHP Code:
    <?php
        $page 
    $_SERVER['PHP_SELF'];    
        
    $sec "10";    
        
    header("Refresh: $sec; url=$page");
        
        function 
    alertFilesAdded($input) {
            
    $inputa $input;
            foreach (
    $inputa as $keyA=>$valueA) {
                if (
    $keyA >1) {
                    
    $keyAA $keyA-1;
                    echo 
    $keyAA." - <font color='red'>".$valueA."</font><br />";
                }    
            }
        }
        
        
    $dir="D:/MyFolder/TestFolder/";

        
    // sort in ascending order - this is default
        
    $a scandir($dir);

        echo 
    "<h2>Files Added</h2>";
        
    alertFilesAdded($a);
    ?>
    Serves my purpose.
    Now am looking for a way, to throw up an alert or for the page to become the active window, as now have to take a look at the page to check for any updates.

    Edit : I have to use the If in the function, otherwise, it shows array keys 0 & 1 as blank & the added file as array key 2. Currently, am unable to see where I am going wrong or why it is happening.

    Thanks
    Last edited by recyan; August 12th, 2013 at 03:31 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    India
    Posts
    1
    Rep Power
    0
    You can try this

    $target_path = "uploads/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem.

    If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Hi,

    Currently, what you have posted, is a bit above me.
    Will try to understand & implement & see how it goes.

    Thanks
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    when you say alert you when a file is uploaded, does this mean that you want to be notified when ANYBODY uploads a file (e.g. through a form).

    1. every time some one uploads a file, you save that in a temp table/file with the filename. When you are browsing, PHP will check that table/file on each page re-load and then write the corresponding js alert function on the fly.

    (or you can do this via ajax/jquery and then do an alert if there is content in that file.)

    In both cases you would want to "delete" contents after they have been processes.

    2. another way would be a cron job, which runs a script every second (or a minute or whatever) that checks files in the folder & sees if there were files added in the last second, minute etc (by checking the creation/modified attribute)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Originally Posted by iplains
    You can try this

    $target_path = "uploads/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    If the upload is successful, then you will see the text "The file filename has been uploaded". This is because move_uploaded_file returns true if the file was moved, and false if it had a problem.

    If there was a problem then the error message "There was an error uploading the file, please try again!" would be displayed.
    iplains, tried the code provided by you, it threw up some errors.
    After, thinking about the code, got a feeling, that it is doing the same thing, as what my code does, albeit, differently & perhaps in a better manner. Correct me, if I am wrong.
    What, I am looking for, is an automatic alert, every time, a new file is added to the folder( an alert after say 10-15 seconds or even after a minute is OK ), to avoid taking a look at my open page again & again.

    Originally Posted by paulh1983
    when you say alert you when a file is uploaded, does this mean that you want to be notified when ANYBODY uploads a file (e.g. through a form).

    1. every time some one uploads a file, you save that in a temp table/file with the filename. When you are browsing, PHP will check that table/file on each page re-load and then write the corresponding js alert function on the fly.

    (or you can do this via ajax/jquery and then do an alert if there is content in that file.)

    In both cases you would want to "delete" contents after they have been processes.

    2. another way would be a cron job, which runs a script every second (or a minute or whatever) that checks files in the folder & sees if there were files added in the last second, minute etc (by checking the creation/modified attribute)
    paulh1983, everything, you say is what is required, except that there is no form, via which, the file is added & there are no tables or db involved.
    Have a folder on my desktop, to which people add files. Need to be alerted, every time a file is added to that folder. My code (posted earlier), displays the files added to the folder. However, does not alert me, when the files are added. I have to look at the page evry now & then, to see, if any files have been added to My Folder.

    Currently, have managed to generate alert by putting it within the "if" in my earlier code. But, naturally, it is not behaving the way I want it to.

    Thanks
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,016
    Rep Power
    376
    i would then go for option 2.. a cron job that looks at the list of the files in the folder.

    btw: How do users upload files to your folder? via FTP?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    128
    Rep Power
    72
    Originally Posted by paulh1983
    i would then go for option 2.. a cron job that looks at the list of the files in the folder.
    Hi,
    Will try to look up "cron job" & see if I can manage.

    Originally Posted by paulh1983
    btw: How do users upload files to your folder? via FTP?
    No, there is no FTP involved. It is a simple 'copy-paste' or 'File-Save As' work.

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo