Hi All I am using Drupal 6.26.
I am trying to upload a file. One user can upload only one file. After a while, if user wants to upload another file, I need to delete the first file and upload the second file. To delete the first file, I do a query to get the first file name, deletes first file and uploads the second file. After a while, if user wants to upload another file, when I do query to get the second file name (as I want to delete the second file now), I always get the first file name. So I am not able to delete the second file.
Probably database cache plays a role here, how can I make sure, my query returns the latest data present in table, query should not pick the data from cache.
Please refer code below.
global $user; $FID = -1;
$result = db_query("SELECT fid, filepath FROM {resubmt_table} LEFT JOIN {files} USING (fid) WHERE resubmt_table.uid = %d", $user->uid); $obj = db_fetch_object($result); if($obj) { $FID = $obj->fid; if($obj->filepath) { if(file_exists($obj->filepath)) { drupal_set_message("File exists deleting " . $obj->filepath); unlink($obj->filepath); } else { drupal_set_message("File does not exists, so not able to delete, file = " . $obj->filepath); } } }
Many times, I am not able to delete the file. Please suggest how can I fix this issue.
Thanks Kamal NECS, Bangalore
You aren't deleting the record from the database, just the file from the file system. You should add in there a:
db_query('DELETE FROM {files} WHERE fid = %d', $FID);
Also do that on any other linked tables you have added in.
Jamie Holly http://www.intoxination.net http://www.hollyit.net
On 8/26/2012 10:25 AM, Kamal Palei wrote:
Hi All I am using Drupal 6.26. I am trying to upload a file. One user can upload only one file. After a while, if user wants to upload another file, I need to delete the first file and upload the second file. To delete the first file, I do a query to get the first file name, deletes first file and uploads the second file. After a while, if user wants to upload another file, when I do query to get the second file name (as I want to delete the second file now), I always get the first file name. So I am not able to delete the second file. Probably database cache plays a role here, how can I make sure, my query returns the latest data present in table, query should not pick the data from cache. Please refer code below. global $user; $FID = -1;
$result = db_query("SELECT fid, filepath FROM {resubmt_table} LEFT JOIN {files} USING (fid) WHERE resubmt_table.uid = %d", $user->uid); $obj = db_fetch_object($result); if($obj) { $FID = $obj->fid; if($obj->filepath) { if(file_exists($obj->filepath)) { drupal_set_message("File exists deleting " . $obj->filepath); unlink($obj->filepath); } else { drupal_set_message("File does not exists, so not able to delete, file = " . $obj->filepath); } } }
Many times, I am not able to delete the file. Please suggest how can I fix this issue. Thanks Kamal NECS, Bangalore
Hi Jamie Holly Thanks for kind reply.
You are right, I just deleted the entry and added a new entry in files table, it looks fine.
I have a small concern. The 'fid' column in files is of type serial. But I do not want unnecessary increment of fid values. For that purpose, while inserting new record, I am just making use of last fid value of that user. Code as below
db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $LastFID, $file->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->status, $file->timestamp);
Hope I am not doing any mistake while re-using last FID value.
Regards
Kamal
NECS, Bangalore
On Sun, Aug 26, 2012 at 8:09 PM, Jamie Holly hovercrafter@earthlink.netwrote:
You aren't deleting the record from the database, just the file from the file system. You should add in there a:
db_query('DELETE FROM {files} WHERE fid = %d', $FID);
Also do that on any other linked tables you have added in.
Jamie Hollyhttp://www.intoxination.net http://www.hollyit.net
On 8/26/2012 10:25 AM, Kamal Palei wrote:
Hi All I am using Drupal 6.26.
I am trying to upload a file. One user can upload only one file. After a while, if user wants to upload another file, I need to delete the first file and upload the second file. To delete the first file, I do a query to get the first file name, deletes first file and uploads the second file. After a while, if user wants to upload another file, when I do query to get the second file name (as I want to delete the second file now), I always get the first file name. So I am not able to delete the second file.
Probably database cache plays a role here, how can I make sure, my query returns the latest data present in table, query should not pick the data from cache.
Please refer code below.
global $user; $FID = -1;
$result = db_query("SELECT fid, filepath FROM {resubmt_table} LEFT JOIN {files} USING (fid) WHERE resubmt_table.uid = %d", $user->uid); $obj = db_fetch_object($result); if($obj) { $FID = $obj->fid; if($obj->filepath) { if(file_exists($obj->filepath)) { drupal_set_message("File exists deleting " . $obj->filepath); unlink($obj->filepath); } else { drupal_set_message("File does not exists, so not able to delete, file = " . $obj->filepath); } } }
Many times, I am not able to delete the file. Please suggest how can I fix this issue.
Thanks Kamal NECS, Bangalore
-- [ Drupal support list | http://lists.drupal.org/ ]
On 8/26/12 9:32 PM, Kamal Palei wrote:
Hi Jamie Holly Thanks for kind reply.
You are right, I just deleted the entry and added a new entry in files table, it looks fine.
I have a small concern. The 'fid' column in files is of type serial. But I do not want unnecessary increment of fid values. For that purpose, while inserting new record, I am just making use of last fid value of that user. Code as below
db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $LastFID, $file->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->status, $file->timestamp);
Hope I am not doing any mistake while re-using last FID value.
Regards
Kamal
NECS, Bangalore
One other option, is if the user has already updated a file, rather than deleting the database record and inserting a new one, do a replace or update query to change the existing one.
Reusing a deleted serial value should be ok too.