Automatically execute script

I have a PHP script that helps me out a lot because it unzips files on my server. We have already automated a process of getting it to the place it needs to do its work. That place being a directory where we have uploaded a zip of a web project. I usually run it by typing the web address of the scripts location on the server. Then I have to choose a couple of options (always the same ones). My question is can this process be setup to run automatically whenever that file is added to a directory? I mean the folder or server will sense its prescience and execute it. Thanks

I don’t know of a way to have the server automatically run the script when it senses the file has been uploaded - that will depend on the server configuration. Someone else will know, I’m sure.

However you could extend the PHP script you already have to handle the upload as well as unzipping the file and deleting it afterwards. So rather than using FTP or whatever, you’d open your script which would prompt you with a “file” dialog box, click the button, PHP script would upload the file, unzip the file, delete the file, then tell you it’s done it.

The trouble with configuring servers to automatically do stuff like that is that if someone were to find a way to upload a malicious file into the directory, it’s probably not a good thing for it to be automatically unzipped without any further intervention.

I don’t see why you couldn’t write a cron job to watch your directory and if a file appears, either email you that something has arrived or automatically run your unzip script.

Very helpful, thank you both!

Yes, both ways would work.

How “time sensitive” is the need and how often does it usually happen?

If it’s not urgent a once-or-twice-a-day CRON would work, but it’s probably not a good idea to have it run every 5 seconds just to check.

I was thinking that it could just happen when the a new file is noticed by the server. If the file is a zip some naming convention appears, it would happen then. I am curious to know if this is the type of thing you do with Node.js? I have just started learning about Node.js Thanks

Possibly, TBH I have no idea if Node could or not.

But I have thought about what the goal is here.

If it is to ensure you’re notified when a zip is uploaded to the server, unless you let others upload by FTP, I think the easiest way would be to modify the upload script to deal with things.

That is, instead of checking to see if a zip has been uploaded, notify you when a zip is being uploaded.

If you want more than notification about an upload happening, as droopsnoot posted, you could have script do the unzipping, but IMHO zip files are often something you want to check to make sure they’re safe first.

1 Like

Humble or not… that is great advice. Could you give some keywords to look in to, for ways you check this zip safety? This is great info! Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.