删除文件 PHP 时出现问题

发布于 2024-12-01 23:05:21 字数 1345 浏览 0 评论 0原文

这是我从 sql 数据库中提取信息的代码,然后我想删除每个目录中的 .txt 文件,但我似乎不明白为什么它不会删除这些文件。

<?php

session_start();
$user = $_SESSION['SESS_USERNAME'];

$id = array();
$id = $_POST['selected'];

//Include database connection details
require_once('config_order.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if (!$db) {
    die("Unable to select database");
}

//Create query
$query = mysql_query("SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'");

// display query results

while ($row = mysql_fetch_array($query)) {
    $c_name = $row['clientname'];
    $sitestreet = $row['sitestreet'];              
    $inspector = $row['inspector'];
}
mysql_close($link);

$client_name = str_replace(" ", "_", $c_name);
$site_street = str_replace(" ", "_", $sitestreet);

$client_name = "{$client_name}.txt";
$site_street = "{$site_street}.txt";

$client_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Clients";
$inspection_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Inspections";

if (unlink($client_path . "/" . $client_name)) {
    echo 'File Deleted';
} else {
    echo 'File could not be deleted';
}
?>

This is my code to pull information from my sql database and then I want to delete the .txt files in each directory, but I can't seem to figure out why it won't delete the files.

<?php

session_start();
$user = $_SESSION['SESS_USERNAME'];

$id = array();
$id = $_POST['selected'];

//Include database connection details
require_once('config_order.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if (!$db) {
    die("Unable to select database");
}

//Create query
$query = mysql_query("SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'");

// display query results

while ($row = mysql_fetch_array($query)) {
    $c_name = $row['clientname'];
    $sitestreet = $row['sitestreet'];              
    $inspector = $row['inspector'];
}
mysql_close($link);

$client_name = str_replace(" ", "_", $c_name);
$site_street = str_replace(" ", "_", $sitestreet);

$client_name = "{$client_name}.txt";
$site_street = "{$site_street}.txt";

$client_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Clients";
$inspection_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Inspections";

if (unlink($client_path . "/" . $client_name)) {
    echo 'File Deleted';
} else {
    echo 'File could not be deleted';
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄暮涼年 2024-12-08 23:05:21

我认为这是因为您的 while 循环覆盖了 $c_name$sitestreet$inspector 变量。这意味着您只能删除最后一个文件。

这就是你想要做的吗? (再次编辑...

$query = mysql_query("SELECT * FROM `PropertyInfo` WHERE `order_number` IN (".mysql_real_escape_string(implode(',',$id)).")");

while ($row = mysql_fetch_array($query)) {

  $inspector = $row['inspector'];
  $client_name = str_replace(" ", "_", $row['clientname']).'.txt';
  $site_street = str_replace(" ", "_", $row['sitestreet']).'.txt';

  $client_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Clients";
  $inspection_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Inspections";

  if (!file_exists($client_path.'/'.$client_name)) {
    echo "File $client_path/$client_name does not exist!\n";
  } else echo (unlink($client_path.'/'.$client_name)) ? "File $client_path/$client_name was deleted\n" : "File $client_path/$client_name could not be deleted\n";

}
mysql_close($link);

I think this is because your while loop is overwriting the $c_name, $sitestreet and $inspector variables. This means you will only ever delete the last file.

Is this what you were trying to do? (Edited Again...)

$query = mysql_query("SELECT * FROM `PropertyInfo` WHERE `order_number` IN (".mysql_real_escape_string(implode(',',$id)).")");

while ($row = mysql_fetch_array($query)) {

  $inspector = $row['inspector'];
  $client_name = str_replace(" ", "_", $row['clientname']).'.txt';
  $site_street = str_replace(" ", "_", $row['sitestreet']).'.txt';

  $client_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Clients";
  $inspection_path = "/var/www/vhosts/default/htdocs/Members/$user/$inspector/Orders/Inspections";

  if (!file_exists($client_path.'/'.$client_name)) {
    echo "File $client_path/$client_name does not exist!\n";
  } else echo (unlink($client_path.'/'.$client_name)) ? "File $client_path/$client_name was deleted\n" : "File $client_path/$client_name could not be deleted\n";

}
mysql_close($link);
﹏半生如梦愿梦如真 2024-12-08 23:05:21

尝试一些额外的调试:

$realpath = $client_path . '/' . $client_name;

if (file_exists($realpath)) {
   if (is_writable($realpath)) {
       if (unlink($realpath)) {
            echo "$realpath deleted";
       } else {
            echo "Unable to delete $realpath";
       }
   } else {
      echo "$realpath is not writable";
   }
} else {
   echo "$realpath does not exist";
}

Try some extra debugging:

$realpath = $client_path . '/' . $client_name;

if (file_exists($realpath)) {
   if (is_writable($realpath)) {
       if (unlink($realpath)) {
            echo "$realpath deleted";
       } else {
            echo "Unable to delete $realpath";
       }
   } else {
      echo "$realpath is not writable";
   }
} else {
   echo "$realpath does not exist";
}
烟雨凡馨 2024-12-08 23:05:21

乍一看,这是一个问题,if $_POST['selected'] 不是一个数组:

$id = array();
$id = $_POST['selected'];

...


$query = mysql_query("SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'");

您正在将 $id 实例化为空数组,然后用 $_POST['selected'] 覆盖它,因此 $id[0] 是字符串 <的第一个字符代码>$id。

例如,如果 $_POST['selected']12345

"SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'"

相当于:

"SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '1'"

要么不要尝试使用索引访问它,要么执行 $id [] = $_POST['selected']; 将元素添加$id 数组中。

无论它是否是一个数组,您确实需要在将输入插入查询或使用

On first glance, this is a problem, if $_POST['selected'] is not an array:

$id = array();
$id = $_POST['selected'];

...


$query = mysql_query("SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'");

You are instantiating $id as an empty array, then overwriting it with $_POST['selected'], so $id[0] is the first character of the string $id.

For example, if $_POST['selected'] is 12345:

"SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '$id[0]'"

is equivalent to:

"SELECT * FROM  `PropertyInfo` WHERE  `order_number` =  '1'"

Either don't try to access it with an index or do $id[] = $_POST['selected']; to add the element onto the $id array instead.

Whether that is an array or not, you do need to either sanitize that input before you insert it into the query or use prepared statements, though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文