php latest upload - php

For various dull reasons, I'd like to assay a script that looks at the files in a directory, copies the filename of the latest and inserts it into a mysql table. It shld also check if the insert has been done already.
I am a web tinkerer (i work in construction) so my question may seem a bit ingenue but what functions do I need to get the filenames of files in a particular directory ? I can see how to check if the insert's been done already plus the db insert bits. I just wanna learn how to get hold of the latest filename.
Afterthought: is there a way to run the script automatically or on completion of a successful ftp upload to the directory in question ?
Tom

I would recommend the SPL DirectoryIterator instead of glob().

To answer your second question:
Afterthought: is there a way to run the script automatically or on
completion of a successful ftp upload
to the directory in question ?
This depends on the type of server you're running on, since you're programming in PHP, I'll assume it's a Unix or Linux based machine, in which case you'll want to read up on CRONTAB, which is the normal way of running scripts at a specific time.
It would be difficult on the server end to know when a client has finished uploading via FTP, as you likely really don't know how many files they might upload.

Related

PHP upload file without having to create a temp

So I am trying to use XAMPP server on ubuntu (Xampp 1.7.7 and yes, I know it is old) to upload files to a specific directory using PHP.
I know that it can be done, but every bit of code I can find wants to use a temp name. I think it checks to see if there is a duplicate file but can't I set it to just go strait to the directory? I know it wont be a problem so is this possible? Let me know if I need to be more specific.
Also, please don't ask "Why don't you want to use the temp directory?" because I don't want/need to is your answer. So if you have an answer, please let me know.
Thanks! :)
PHP couldn't care less about duplicate files. If two people upload "file.txt", PHP won't care, because it'll be using that nice random temp name instead. File collision handling is NOT php's job. that's up to your code.
And while it would be nice if you didn't HAVE to use a temp file for uploads, removing that restriction would mean a complete re-write of PHP's upload infrastructure. The script which a file upload is performed on is not invoked until AFTER the upload has completed (regardless of success). There is no mechanism in PHP to allow a "live" script to accept the upload as the bytes are streaming in.
If you need to handle the raw straw as the upload proceeds, you'll have to use some other language, e.g. perl.

Copying the actual script content of a PHP file and not the executed result

What I have to be able to do is copy an entire folder from a remote source to the local server executing the PHP file. I can do that fine except for one problem, PHP files. Obviously, I can't just go copying the source code of a PHP file using regular commands as they will interpret the code and give me the returned stuff. What I have to have is the code. Is there any way to do that?
Hope I'm clear enough, my problem isn't something very hard to understand, I just want to know if it's actually possible. If not, maybe someone may have an idea of an optimal way of storing the source code alongside the executable php? I was thinking simply saving it as text when I'm done developing but if there is a way to do it completely automatically then that would be much more awesome. Best case scenario, I can just copy the folder with php files and then execute it from local. I need to know if that's even possible... Worst case scenario, I have to duplicate files in order copy the text version of them to the local server and discard the php ones since th e executed files are not relevant to my program. I don't want that, but I just don't know if PHP is able to do what I want.
Edit: sorry for not specifying! I am the admin of the remote server and have total access. I can and was expecting to make a php file on the server itself. That's the kind of system I have at the moment! I zip a folder and return it when requested from my local source. My only problem is the php executing.
You cannot do that unless you:
have FTP access (or anything else that is not HTTP-based)
have access to a script on the server that is designed to return the sourcecode of a given file
use an exploit such as the ?-s bug in the CGI SAPI
So you are most likely out of luck.

Best practices to let my web users download custom .exe from my site using PHP

im trying to implement on my site a system who let the user download a file that have to be change before the download.
I have a master file (a .exe program), that inside have a variable who has to be change for every different user.
The most simple solution is to change a variable inside a xml file every time the user want his personalized exe and then make the exe file to read the external file. BUT i dont want the user to download more than one file.
Is this possible? using php can i change a parametter inside a compiled program? Thanks for any help and suggestions!
If you really really know what you're doing and you know exactly the bits that need to be flipped inside the file, then yes, it's possible to modify the .exe file with PHP. Otherwise you have to make changes to the source or other files the .exe is built with and compile the program on the server before sending it to the user.
In theory it's certainly possible (PHP is turing complete), but as stated in other answers it will be hardly worth the hassle (considering the fact that you have to ask whether it is possible shows you'd have to investigate at last for days into the standard exe-format).
I'd recommend one of the following:
1) Zip the program with the configuration file; either use a separate launcher (e.g. Java [a JAR is a ZIP file]) or add a configuration file that is read by the program itself. There is a number of PHP libraries for generating ZIP files.
2) compile the program with the changed source on the server itself; however this can also become quite complicated depending on your server configuration and the programming environment you use. If you have never administered a virtual server I would not even slightly recommend that as an option.
3) If you can assume that the user got somewhat stable Internet access you might also consider to let hir download a standard executable, where additional configuration will be downloaded later on by the program itself (e.g. by transmitting the username to the server). However this creates dependencies you might want to avoid (your user probably can't use it on machines without Internet access and you should assert that your server is up most of the time).
While it's probably possible, I doubt it's worth the hassle. Unless you're trying to fight piracy or something. Why don't you just serve the user a custom .zip file with the .exe and a config .xml?
[edit after OP commented]
I presume what you're trying to edit is the facebook ID/username? Well, try to compile the base file with some unique string like "THISNEEDSTOBEREPLACED", then use some binary safe function to replace it. Though remember things can and will get tricky if the string lengths don't match.

Is it possible to create a self-installing PHP framework?

Ok this might seems a bad idea or an obvious one. But let's imagine a CMS like PHPBB. And let's imagine you'd build one. I'd create just 1 file called PHPBB.install.php and running it it will create all folders and files needed with PHP. I mean, the user run it just once and every file and folder of the app is created via the PHP file.
Why to do this?
Well mostly because it's cleaner and you are pretty much sure it creates everything as you wish (obliviously checking everything about the server first). Also, having all the files backed-up inside a file you would be able to restore it very easily by deleting everything and reinstalling it running again PHPBB.install.php. Backing-up files like this will allow you to also prevent errors: How? When an error occurred in a file, this file is restored as it was and automatically re-run.
It would be too heavy!
The installation would happen only once and you'd be sure the user will not forget to place the files correctly. The error-preventing will worth the cause and it would also happen only once.
Now the questions:
Does this technique exists? If so, What's its name?
Why would you discourage it?
As others have said, an installer.
It requires the web server to have permission to write to the filesystem, and ends up having the files owned by the user the web server runs as. Even when one has the ability to change filesystem permissions, it's usually a longer process than just extracting an archive and having the initial setup verify permissions.
Does this technique exists? If so, What's its name?
I'd advise to read about __halt_compiler(). It allows you to mix PHP code with non-php data which is not parsed, so you may have PHP code ("installer") and binary data (e.g., compressed contents of all the files) in single PHP file.
1 - Yes, there is a single install file in PHPBB. You run through an online wizard defining your settings and then it installs automatically.
http://www.phpbb.com/support/documents.php?mode=install&version=3&sid=908f5766fc04868ccb985c1b1e6dee4b#quickinstall
2 - The only reason to discourage it would be if you want the user to understand exactly how the system works. Automatically installing it means the user has no need to understand the nitty gritty of it all - of course, many see this as a good thing.

How to synchronize local directory with remote ftp directory?

Our website relies on images from one of our manufacturers. The image directories are massive and getting them via FTP is an all day job. Now that we've downloaded the entire directory, we'd like to be able to periodically download files and directories that are new, or have been changed since the last time we downloaded them. We're thinking about writing a script that checks the modification date of files and only downloads the latest versions.
Since this can't be the first time this problem has been encountered or solved, I thought I'd post this and see if anyone knows of existing solutions that can be applied here. An existing solution would need to be compatible with FreeBSD and/or LAMP.
Is there any reason you can't use rsync?
with wput
As user77413 noted in another comment, this should work...
wget --mirror username:password#siteurl.com/path
The default number of retries is 20, you can increase this with --tries 100

Categories