简体   繁体   English

我正在尝试将批处理嵌入到 php 中,但它没有执行

[英]I'm trying to embedded batch into php, but it is not executing

So, I'm trying to setup a batch executable inside a website (php in this case), so it would download certain file directly to desired directory, without need for user to interact with it.因此,我正在尝试在网站(在本例中为 php)中设置一个批处理可执行文件,因此它会将某些文件直接下载到所需的目录,而无需用户与之交互。 Basically the plan is if there was a website with mods/in-game builds/worlds for a game, you'd want to download them directly into AppData, and not bother with moving it from Downloads manually.基本上计划是,如果有一个带有游戏模组/游戏内构建/世界的网站,您希望将它们直接下载到 AppData 中,而不必费心从下载中手动移动它。 I am using Xampp localhost to test run it (and I did run it as admin).我正在使用 Xampp localhost 来测试运行它(我确实以管理员身份运行它)。

I searched up online to find how to embed batch inside php, and got to this:我在网上搜索以找到如何在 php 中嵌入批处理,并得到了这个:

<?php
    exec("cd %AppData% && curl <LINK> -o <NAME>.<FILE_SUFFIX>");
?>

I tried with 'system' instead of 'exec', adding 'cmd /c' in front of the command as well, but not working either我尝试使用 'system' 而不是 'exec',也在命令前面添加了 'cmd /c',但也不起作用

I tried a different approach after that, just to test之后我尝试了不同的方法,只是为了测试

<?php
    exec("start batch.bat");
?>

with this code用这个代码

@echo
cd %AppData% 
curl <LINK> -o <NAME>.<FILE_SUFFIX>
pause

Which resulted in这导致

'curl' is not recognized as an internal or external command, operable program or batch file.

I also tried absolute path instead of relative, but no positive result either.我也尝试过绝对路径而不是相对路径,但也没有积极的结果。

Now I don't know what else to try and what could be causing this.现在我不知道还有什么可尝试的以及可能导致这种情况的原因。 If there is another viable option to achieve what I've stated above, please do let me know as well.如果还有其他可行的选择来实现我上面所说的,也请告诉我。

So here's the working code I am using now所以这是我现在使用的工作代码

<?php
    exec("bitsadmin /transfer myDownloadJob /download /priority high <LINK> <TARGET_LOCATION_FILE>");
?>

I still don't know why the curl didn't work, but as bitsadmin is native windows command, it's better anyway.我仍然不知道为什么curl不起作用,但由于bitsadmin是本机 Windows 命令,无论如何它更好。 Thanks to everyone who helped!感谢所有帮助过的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM