sonnieboy Posted August 11, 2015 Share Posted August 11, 2015 (edited) // Check to see if any images need deleting if(isset($_POST['delete']) && !empty($_POST['delete'])) { // whilelisted table columns $fileColumnsInTable[] = 'BidIDFile'; $fileColumnsInTable[] = 'TabSheet'; $fileColumnsInTable[] = 'SignInSheet'; $fileColumnsInTable[] = 'XConnect'; $fileColumnsInTable[] = 'Addend1'; $fileColumnsInTable[] = 'Addend2'; $fileColumnsInTable[] = 'Addend3'; $fileColumnsInTable[] = 'Addend4'; $fileColumnsInTable[] = 'Addend5'; $fileColumnsInTable[] = 'Addend6'; // Loop through the post to assign delete fields foreach($_POST['delete'] as $fileCol => $fileColumn) { // If set and allowed to be delete if(in_array($fileCol, $fileColumnsInTable)) { // Save the columns to temp $fileColumns[] = $fileCol; // Save the file spots as blanks in the main post $_POST[$fileCol] = ''; } } // Check that there are files requiring attention if(isset($fileColumns)) { $sql_statement = "select ".implode(", ",$fileColumns)." from bids where ID = ?"; $query = sqlsrv_query($conn,$sql_statement,array($strId)); // No files, just return if($query === false) return $errors = 'file_fail_link'; $files = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); //print_r($files); // loop over the files returned by the query foreach ($files as $file) { $thisfile = __DIR__.'/uploads/'.$file; //delete file if(is_file($thisfile)) unlink($thisfile); } } } if(isset($_POST['delete'])) unset($_POST['delete']); { //echo "This is an update statement"; // This will generate an update query $update = $BidSet->SaveData($_POST,array("Id")) ->SaveFolder('../uploads/') ->AddFiles('BidIDFile') ->AddFiles('item') ->AddFiles('SignInSheet') ->AddFiles('TabSheet') ->AddFiles('Xcontract') ->where(array("Id"=>$_POST["Id"])) ->UpdateQuery() ->statement; //echo '<pre>'; //print_r($update); //echo '</pre>'; //***************************************************************************************************** <td> <table border="0"> <tr> <td class="td_input_form"> <?php // if the BidIDFile is empty, if(empty($result["BidIDFile"])) { //then show file upload field for Bid File echo '<input type="file" name="BidIDFile[]" size="50">'; } else { // Bid file already upload, show checkbox to delete it. echo '<input type="file" name="BidIDFile[]" size="50"> <br />'; echo '<input type="checkbox" name="delete[]" value="'.$result["BidIDFile"].'"> (delete) <a href="http://KitApps/train/Boards/uploads/'.$result["BidIDFile"].'" target="_blank" onclick="window.open (this.href, \'child\', \'height=800,width=850,scrollbars\'); return false" type="application/octet-stream">'.$result["BidIDFile"].'</a>'; } ?> </td> </tr> </table> </td> Dear all, I have some code snippets. Complete code is very long. The code does insert and update. You can unlink (delete) a file only without updating any records or you unlink a file as well as update records. This has worked very well before but suddenly, it is no longer unlinking a file but record updates still work fine. Any ideas why? Please see code snippet. Thanks a lot in advance. Edited August 11, 2015 by sonnieboy Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/ Share on other sites More sharing options...
QuickOldCar Posted August 11, 2015 Share Posted August 11, 2015 If the current code worked before and was no changes to it...is most likely something else, file permissions maybe? Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518487 Share on other sites More sharing options...
sonnieboy Posted August 11, 2015 Author Share Posted August 11, 2015 First of all, thanks for the prompt response. That was my initial thought but I check file permissions and that was not the problem. Besides, we could save files to the folder. It is just that we are no longer able to unlink any files. Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518488 Share on other sites More sharing options...
iarp Posted August 11, 2015 Share Posted August 11, 2015 I would echo $thisfile to see what it is trying to delete and see if that path actually exists. Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518489 Share on other sites More sharing options...
sonnieboy Posted August 11, 2015 Author Share Posted August 11, 2015 Great suggestion, iarp!! This is what it shows: E:\inetpub\wwwroot\Train\Boards\admin/uploads/0 The path should actually be: E:\inetpub\wwwroot\Train\Boards/uploads/ And the issue is here: $thisfile = __DIR__.'/uploads/'.$file; How do I change this to correspond with E:\inetpub\wwwroot\Train\Boards/uploads/ Thank you Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518506 Share on other sites More sharing options...
QuickOldCar Posted August 11, 2015 Share Posted August 11, 2015 If magic constants is actually enabled it returns that folders directory path You can define it if it's not. if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } if you have some sort of rewrite rule for the site the code is still seeing it's original path. You can easily set this manual versus using __DIR__ $thisfile = 'E:\inetpub\wwwroot\Train\Boards/uploads/'.$file; Hard to suggest anything beyond that because do not know anything else about your file structure or rewrite rules. Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518512 Share on other sites More sharing options...
sonnieboy Posted August 11, 2015 Author Share Posted August 11, 2015 Why is the result looking like this? E:\inetpub\wwwroot\Train\Boards/uploads/0 Notice the 0? Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518518 Share on other sites More sharing options...
QuickOldCar Posted August 11, 2015 Share Posted August 11, 2015 (edited) What is the $file value? It must not be returning results from the query and the 0 is the false value. Edited August 11, 2015 by QuickOldCar 1 Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518523 Share on other sites More sharing options...
sonnieboy Posted August 12, 2015 Author Share Posted August 12, 2015 Array ( [] => 0 ) // Check to see if any images need deleting if(isset($_POST['delete']) && !empty($_POST['delete'])) { // whilelisted table columns $fileColumnsInTable[] = 'BidIDFile'; $fileColumnsInTable[] = 'TabSheet'; $fileColumnsInTable[] = 'SignInSheet'; $fileColumnsInTable[] = 'XConnect'; $fileColumnsInTable[] = 'Addend1'; $fileColumnsInTable[] = 'Addend2'; $fileColumnsInTable[] = 'Addend3'; $fileColumnsInTable[] = 'Addend4'; $fileColumnsInTable[] = 'Addend5'; $fileColumnsInTable[] = 'Addend6'; // Loop through the post to assign delete fields foreach($_POST['delete'] as $fileCol => $fileColumn) { // If set and allowed to be delete if(in_array($fileCol, $fileColumnsInTable)) { // Save the columns to temp $fileColumns[] = $fileCol; // Save the file spots as blanks in the main post $_POST[$fileCol] = ''; } } // Check that there are files requiring attention if(isset($fileColumns)) { $sql_statement = "select ".implode(", ",$fileColumns)." from bids where ID = ?"; $query = sqlsrv_query($conn,$sql_statement,array($strId)); // No files, just return if($query === false) return $errors = 'file_fail_link'; $files = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC); print_r($files); I am really confused now. How did this work before? When I run above code and do print_r($files); I get this: Array ( [] => 0 ) How did this work before? What changed? Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518547 Share on other sites More sharing options...
QuickOldCar Posted August 12, 2015 Share Posted August 12, 2015 Check to see if any of your database tables are corrupt and repair them. Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518553 Share on other sites More sharing options...
mac_gyver Posted August 12, 2015 Share Posted August 12, 2015 your query is matching no rows. where is $strId coming from and what does echoing/dumping its value show? your code should test if the query found any rows before trying to use the data from the query. 1 Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518566 Share on other sites More sharing options...
sonnieboy Posted August 12, 2015 Author Share Posted August 12, 2015 if(isset($_POST["Id"])) { $strId = $_POST["Id"]; //echo $strId; } If ($strId == "") { //echo "This is an insert statement"; // This will generate an insert query $insert = $BidSet->SaveData($_POST) ->SaveFolder('../uploads/') ->AddFiles('BidIDFile') ->AddFiles('item') ->AddFiles('SignInSheet') ->AddFiles('TabSheet') ->AddFiles('Xcontract') ->InsertQuery() ->statement; // Check that statement is not empty if($insert != false) { sqlsrv_query($conn,$insert); ?> <br><br><br><br><br> <div class="error_button"> <div style="float: right; font-size: 18px; font-weight: bold; color: #FFF;" onClick="DisplayErrorTag('close')">x</div> <?php render_error(array("title"=>"Bid Successfully Saved!","body"=>'Go back to <a href="currentrecs.php">Solicitation screen</a>')); ?> </div> <?php $err = false; } //echo '<pre>'; //print_r($insert); // echo '</pre>'; } else { // Check to see if any images need deleting --- --- Hi guys, thanks again for your help. With mac_gyver first, the $strid is coming from the top of the code above. When I echo it, it shows the correct Id. QuickOldCar, neither the database nor any of the tables is corrupt. Quote Link to comment https://forums.phpfreaks.com/topic/297735-unlinking-a-file-suddenly-stopped-working-any-ideas-why/#findComment-1518599 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.