I will be sending new files over from one computer to another computer. How do I make PHP auto detect new/updated files in the folders and enter the information inside the files into mysql database?
Get all files you already know from the database
loop through the directory with http://www.php.net/manual/de/function.readdir.php
if the file is known, do nothing
if the file is not known, add it to the database
In the end, delete all files no longer in the directory
I would pick a set-up where new files and old fields are in a separate directory.
But if you have no choice, you could check the modification date and match it with your last directory iteration. (Use filemtime for this).
Don't forget to do some database checking when you process an image though.
Save the timestamp of the last check and when you check next look at the fileinfo and check creation date. Even better yet because you store filecontens in a database, check for the time it was modified using: filemtime()
You can't. PHP works as a preprocessor and even it has execution time limit (set in the configuration). If you need to process with PHP then make a PHP script that outputs a web page that use meta redirection to itself. Inside the script, you should loop over the files, query the database for the file name and its modification time, if it exists then nothing to do, otherwise, if the file name exists then it's an update, otherwise it's a new file.
Related
In my WordPress site, I have a script that looks at a directory and uploads each image in that directory to a specific post. Right now, this is set to happen each time the user opens the post edit page. I need a way to check if the contents of the directory have changed. That way, I can set the script to not run if the contents of the directory have not changed. Is there a way to accomplish this in PHP?
I would try to make a hash of directory content, let's say concating the names of files (sorted) with some divider and making md5/sha1 hash. You'll need to store it in some way (in directory as text file or database).
On access you'll have to calculate actual hash of directory in the same way and compare it with the old (saved) one. Depending on result, you can take some actions...
You can check the modification date of a file with filetime
http://php.net/manual/de/function.filemtime.php
A solution is to store the day of the last visit and look for younger files at the next visit.
You can open every file in PHP and generate checksums with MD5, then save this with time() to text file in directory. Next time you can compare yours checksums and you will know if anthing has changed since last time.
I have hundreds of mp3 files on my server. Each file's modified-date is important because it is fetched by PHPs filemtime to represent it's upload date (since there's no way to determine an upload time without storing values in a database).
I have come across an audio issue in which all the files need to be normalized and re-uploaded to the server. This would, of course, change the modified-date of each file to "today". I need each file to retain it's original modified-date.
I'm not sure if this is a software-recommendation question or a programming question, so I apologize if this is the wrong .SE site. Is this even possible?
You should be able to set the modified time with touch: http://php.net/manual/en/function.touch.php
This requires PHP > 5.3 and the user running the script (probably your web user unless you run it from the cli) needs to have write permission on the file.
You have two options for implementation:
Store the filenames and their mtimes in temporary storage (either a file or a database table). When you finish the upload, run through all of the files and use touch to reset the mtime.
As you upload the files, check to see if the file already exists. If it does, grab the mtime in a temporary variable, overwrite the file, then touch it with the correct mtime.
I know this isn't the answer you're looking for, but it would make far more sense to start storing this information in a database than relying on the last-modified date. This way you can show your users the date that they need to know and retain the true date of modification.
An approach like this also gives you much more flexibility.
As requested by #Snailer - for the sake of closing the question.
So I'm trying to see if something like this is possible WITHOUT using database.
A file is uploaded to the server /files/file1.html
PHP is tracking the upload time by checking last update time in database
If the file (file1.html) has been updated since the last DB time, PHP makes changes; Otherwise, no changes are made
Basically, for a text simulation game (basketball), it outputs HTML files for rosters/stats/standings/etc. and I'd like to be able to insert each team's Logo at the top (which the outputted files don't do). Obviously, it would need to be done often as the outputted files are uploaded to the server daily. I don't want to have to go through each team's roster manually inserting images at the top.
Don't have an example as the league hasn't started.
I've been thinking of just creating a button on the league's website (not created yet) that when pushed would update the pages, but I'm hoping to have PHP do it by itself.
Yes, you could simply let php check for the file creation date (the point in time where the file was created on the server, not the picture itself was made). check http://php.net/manual/en/function.filemtime.php and you should be done within 30mins ;)
sexy quick & dirty unproven code:
$filename = 'somefile.txt';
$timestamp_now = time(); // get timestamp from now (seconds)
if (filemtime($filename) > $timestamp_now) {
// overwrite the file (maybe check for existing file etc first)
}
I am working on an application, where the user has to upload files in a cart.e.g. the user upload the file "A" and now doing different work. After some time he again upload another file, file "B". How I can manage the file path or store the file path, as if I use move_uploaded_file() function, then it can overwrite the other user's file with same file name.
Thanks.
When I've had this issue, I have used a timestamp added to the filename. Usually I want to cleanse the filename anyway, so I
replace characters I don't like
remove the file extension and check it looks OK (e.g. pdf not exe)
add a timestamp to the filename
put the extension back on
Obviously, this isn't suitable in every instance, but it might give you some ideas.
Create folder run time against session id, that way only current session user files goes to the folder.
temp_uploads/ (main uploads folder)
temp_uploads/_jhk43543h5h435k3453 (session id folder for user 1)
temp_uploads/_jhk43543h5h435k34tr (session id folder for user 2)
temp_uploads/_jhk43543h5h43trtrtg (session id folder for user 3)
you just need store session id for each user, which you maybe you are already doing.
happy Coding :)
You use php's time function to generate a timestamp that you append to the filename so that they can be different. Then you can use a column in the db to store the file paths. You could store all the file paths in the same column but separate each one with ; or any other character. To get the separate paths, you can use php's explode function.
I am working on a database program using PHP to keep track of the products we manage at my workplace.
For this project, I need to be able to select an .XLS file which contains new product data.
New data consists of the following fields:
Type CHAR(3),
Line CHAR(2),
Number INT,
Measure INT,
Comments VARCHAR(255),
Variation CHAR(1) i.e.('Y' || 'N')
These files are created in Excel, or Google Docs; I have found a wonderful excel_reader which allows me to extract the values from this file.
As this is an action which will happen routinely, as new products are created, so I do not want the file to be stored in my server directory (after a while there would be dozens!).
I would rather that the file simply be read, because the import script I'm writing transfers the file's data into an array.
What I really want to happen is to have the user select the file's location (on their local computer) through an HTML form, and then have the script save that file's contents to a MySQL database without ever sending the file to the Server.
I would greatly appreciate any advice you can offer me, I'm not even sure that my plan is a valid way to handle this situation.
It will have to be stored, at least temporarily. Delete the file after you have what you need from it (presumably after moving it out of the temp directory using move_uploaded_file, to the folder from which you will read it), then remove it using unlink.
As a last point, I would be a little worried about immediate deletion of uploaded files. What if something goes wrong with the script while the file is being parsed and data stored in the database? It would probably be a good idea to have a cron job that periodically deletes the files, to be on the safe side, instead of deleting them immediately.
Since your PHP script is running on the server, the Excel file will have to be saved to the server to be read. Once you've read the file and stored it in the database, just delete it.
I found the answer to my question here. It is a nice tutorial on uploading, moving, reading, and deleting files using PHP.
Thank you to all who contributed.
I was struggling to do the same (for xlsx though).
The solution is to use the $_FILES['file']['tmp_name'],
where file is the input name.
Regards :)