【问题标题】:Can we get php value dynamically from URL to a batch file?我们可以从 URL 动态获取 php 值到批处理文件吗?
【发布时间】:2019-11-28 11:11:16
【问题描述】:

这是我的批处理文件脚本,我正在通过任务调度程序运行此脚本 通过使用以下脚本,我可以将 invoice_feed 文件夹中的所有文件复制到网络共享测试文件夹(shared_folder),两台服务器都通过 VPN 连接

Echo off
net use \\windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed \\windows_servername\\test_folder

如果我想将特定文件复制到网络共享文件夹,那么我将手动将该文件添加到上述批处理脚本中,它运行良好。下面是代码

Echo off
net use \\windows_servername\test_folder shared_folder_password /user:shared_folder_username
xcopy C:\Apache\htdocs\Invoices\invoice_feed\file_to_copy.csv \\windows_servername\\test_folder

我可以使用 PHP 运行这个脚本吗?如果是,那么我可以从 URL 传递动态值并传递它来代替下面的特定文件名吗?

xcopy C:\Apache\htdocs\Invoices\invoice_feed\<?php echo $_GET['csv_file_name']?> \\windows_servername\\test_folder

或者有没有其他方法可以代替我的意思是完全使用 php 在批处理文件中执行此操作? 我正在尝试运行如下脚本

$file_name = $_GET['csv_file_name'];
echo exec('net use \\windows_servername\test_folder shared_folder_password /user:shared_folder_username');
echo exec('xcopy C:\Apache\htdocs\Invoices\invoice_feed\$file_name \\windows_servername\\test_folder');

但没有运气。它显示 0 个文件已复制。我哪里做错了?

【问题讨论】:

  • 非常不清楚您要问什么。在执行此批处理脚本和 PHP 之间,这里甚至有任何 any 连接吗?您是否以某种方式调用此 via PHP?如果是这样,请显示该部分。
  • 我已经更新了我的问题
  • @04FS ,我已经编辑了我的问题并更新了我的 php 脚本。请检查

标签: php server copy vpn


【解决方案1】:

在批处理文件中,您可以传递参数并捕获该参数。

batchname.bat arg1 arg2 在批处理文件中,您可以捕获如下参数。

set arg1=%1
set arg2=%2
xcopy C:\Apache\htdocs\Invoices\invoice_feed\%arg1% \\windows_servername\\test_folder

因此,如果您从 php 触发此批处理,则可以轻松地将 get 参数作为参数传递给批处理文件。

您可以使用system 函数运行批处理文件。

$last_line = system("cmd /c C:/path/to/bat/batchname.bat ".$_GET['csv_file_name'], $ret_val);
echo 'last line: '.$last_line;
echo 'return values: '.$ret_val;

【讨论】:

  • 如何使用php将值传递给arg1,arg2?你能告诉我示例代码吗?上面我已经用我正在使用的 php 脚本更新了我的问题?
  • @jagad89我收到“命令已成功完成。”在浏览器上。但文件未复制到网络文件夹。
  • 你的文件名有空格吗?
  • 文件名中没有空格。但是当我直接在批处理文件上给出文件名时,它正在工作。
  • 它正在使用 exec 命令。不是像下面这样的系统命令 exec('C:\\scripts\batch1.bat '.$_GET['FILE_NAME']);
猜你喜欢
  • 1970-01-01
  • 2016-07-05
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2014-12-31
相关资源
最近更新 更多