【问题标题】:File Unlinking not Working when PHP Delete Link is Pressed按下 PHP 删除链接时文件取消链接不起作用
【发布时间】:2014-03-26 18:33:27
【问题描述】:

我正在尝试使此链接正常工作:

<a href='{$_SERVER['PHP_SELF']}?del=true&product_code=".($row['product_code'])."' style='color:black;' onclick='return show_confirm();'>Delete</a> 

它使用 while 函数从 MSSQL 表中删除指定的行。目前,底部代码可以正常工作,因为它从 MSSQL 表中删除了特定行,但我希望它还取消链接 img 文件夹中的一个文件和 specsheets 文件夹中的另一个文件。

img 取消链接的文件的文件名存储在该表行的product_img_name 列中,另一个文件的文件名存储在specsheet 列中。每个表行都有这两列,其中包含来自位于 imgspecsheets 文件夹中的文件的唯一文件名。

到目前为止,当我点击删除时,我收到了这些错误

Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'G1013' to data type int. (severity 16) in D:\Hosting\dl\partscatalogue\partscataloguemanagement.php on line 1175

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hosting\dtscatalogue\partscataloguemanagement.php on line 1175

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\Hostingdtml\partscatalogue\partscataloguemanagement.php on line 1175

Warning: unlink(img/) [function.unlink]: Permission denied in D:\Hostingdml\partscatalogue\partscataloguemanagement.php on line 1177

Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'G1013' to data type int. (severity 16) in D:\Hostingdml\partscatalogue\partscataloguemanagement.php on line 1179

Warning: mssql_query() [function.mssql-query]: Query failed in D:\Hostindhtml\partscatalogue\partscataloguemanagement.php on line 1179

Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\Hosting\dhtml\partscatalogue\partscataloguemanagement.php on line 1179

简单来说,我的问题是,当表格行被删除时,我网站的img 文件夹中该行的文件和我网站的specsheets 文件夹中该行的文件仍然存在,不会随该行一起被删除.

这是针对特定行点击删除链接的代码:

// delete from table
if ($_GET['del'] == 'true') {
   // cast id as int for security
   $product_code = $_GET['product_code'];
   $fileas = mssql_fetch_array(mssql_query("select product_img_name from products where product_code = $product_code"));
     if (file_exists("img/$fileas")) {
   unlink("img/$fileas");   
     }
   $file38 = mssql_fetch_array(mssql_query("select specsheet from products where product_code = $product_code"));
     if (file_exists("spechsheets/$file38")) {
   unlink("specsheets/$file38"); 
     }
   // delete row from table
   $sql = "DELETE FROM products WHERE product_code = '$product_code'";
   $result = mssql_query($sql, $conn) or die();
   } // end if del

感谢您的帮助。非常感谢所有帮助。

【问题讨论】:

  • if (file_exists($fileas)) {unlink("img/$fileas"); } -- 你正在检查一个路径,然后取消链接一个完全不同的路径.. $fileas !== img/$fileas
  • 还可以查看 php.net 上的一些 comments 关于函数 unlink 的内容。 unlink 函数可能没有按照您的想法执行。此外,unlink 函数在成功时返回 True,在失败时返回 False;您可以在删除 MySQL 中的条目之前检查 unlink 是否可以作为检查
  • 我已经更新了我的问题并更新了您在上面陈述的代码。

标签: php sql sql-server unlink


【解决方案1】:

我不确定您的目录结构是什么,但例如您正在检查 $fileas 是否存在,然后尝试取消链接 img/$fileas,这不是同一个地方。如果 img/$fileas 是正确的路径,这也是您需要放入 file_exists() 以获得正确检查的内容。所以它会是 file_exists(img/$fileas) 等等。

【讨论】:

  • 我更正了文件路径,但仍然出现错误。我已经更新了问题中的代码/错误。
  • 我从未使用过 Mssql,所以我假设它在这里的工作方式类似于 MySql。我假设 product_img_name 是您要获取的实际文件名?目前 $fileas 是从数据库返回的第一行的数组(在这种情况下只有行,如果您的数据库设置为 product_code 作为唯一字段),要访问 product_img_name 的数据,您需要使用 $fileas ['product_img_name']。你可以在 $fileas['product_img_name'] 上做一个回显,以确保它得到你想要的。我相信 File_exists 和 unlink 都需要文件扩展名。
猜你喜欢
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多