dongtan1009 2011-09-20 23:51
浏览 29
已采纳

如何在PHP中保存文件并根据可能的故障将文件特征保存到数据库

I am saving a file to a server and I'm saving the filename, user id of the individual who uploaded the file, and various other information about the file to a PostgreSQL database. I ideally want to structure this in a way that is similar to a database transaction. That is, one of the following should occur:

  1. Save file
  2. If successful, save information to database
  3. If saving information to database fails, delete file

OR

  1. Save information to database
  2. If successful, save file
  3. If saving file fails, delete information from database

The only problem with this methodology is step 3 may fail--that is, you may be left with a file saved without corresponding information in the database or you may have database information without a corresponding file saved.

What I want to know is if there is a way that you can use either of the two methods above while ensuring step 3 always occurs or occurs with sufficient reliability that I effectively do not have to worry about failure.

Note that I am using a PostgreSQL database and saving an image file using the imagegif, imagejpeg, and imagepng functions from the GD library.

EDIT

In response to Jack's solid comments below, I forgot to mention that I am interacting with the DB through an API server, so instead of directly interacting with the DB, I am sending REST requests to the API which in turn interacting with the DB. What this effectively means is that I can't just put both database interactions into one transaction.

  • 写回答

2条回答 默认 最新

  • dou7466 2011-09-21 22:28
    关注

    I decided to choose a sub-optimal but still workable route of running a periodic check that searches for database entries for every filename and if a database entry cannot be found, then the file is deleted. (If anyone is actually interested in the code details, I will gladly provide it in an edit.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题