To avoid use of Database when managing files and pictures seems to be a big problem. And its true, use of dadabase is not good for large files.

For example.

Assume you make a discussion board, just like this one using PHP and MySQL. The forum allows users to upload their pictures for every post.

Of course the pictures don't go to the database but to one folder. The database just holds only the name of their picture file.

One day lets say the user decides to delete his post with the images. So in fact he orders PHP to excecute 2 separated tasks.

1st Task. Delete Pictures from the folder.
2nd Task. Delete Post from Database.

Lets say that his computer crashes or he switches his computer off at the middle of the tasks so that only 1st task has been executed but not the second.

So next time the post because useless, and forums become messed up with time.

How can we avoid issues like this?

Thanks
Alex.

    Well, it's very unlikely to occur, I mean when a script is executed you except that it will be executed from the beginning to the end.
    Your example is an exception, and why don't you imagine a script that you will execute as a batch that clean your database

      Yes, your example describes the ituation when the server crashes. Of course, server crash can lead to unpredictable problems. But as we speak about a forum, not a bank accounting system, we may consider such situations rare enough not to worry about it.

      In banking system we, of course, have to have transactions and backups etc.

      BTW, you can delete images & posts in other order:
      first you delete post, and then images. In case script crashes between DELETE query and unlink() function, you can write administrative script (and put it on cron-tab), that will once a day go through image folder and delete all the images, that have no posts pointing to them.

        Hi Wien and the_Igel.

        Thanks for your answers.

        From what I understand, in the server, PHP script is executed from the beginning to the end, regardless if the user switch off his computer at the middle of it.

        Do I understand right?

        Also your idea the_Igel is very good, for the administrative thing.

        Alex

          You see, after a client sends a http request for a page to the server, the server starts to generate that page (in case it's a script). It has a got a request, and it has to handle it. The server doesn't even know, are you still alive.... You may close your browser, turn off the computer, chop the phone line in little pieces, but server will exeute the script 🙂

            This is good news.

            Even if the guy wants to throw his computer out the window, the script will continue to be excecuted. Funny enough, but this makes me feel happy.

            Alex


              BEGIN TRANSACTION;

              DELETE PICTURE FROM DATABASE;

              unlink($file);

              if unlink fails, then ROLLBACK else COMMIT:

              Chris

                To chriskl:
                The arguing is not about situation when unlink fails, but when the server crashes.
                It can as well crash after unlink() but before COMMIT. Therefore, no big difference.

                  allmost got this thing running..
                  the files are stored on the harddisk instead of in a database. during upload file information is saved in the database..

                  another script loads the file info in a list... whatever..
                  just see it for yourself..
                  http://dwarv.cjb.net/

                  sorry for the ads...

                    Write a Reply...