1

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. 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. I am using Xampp localhost to test run it (and I did run it as admin).

I searched up online to find how to embed batch inside php, and got to this:

<?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

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.

  • 5
    curl is a program that needs to be on your computer. If you can't run these commands from your computer's command line they won't work when called from php like this either. Try installing [curl for windows](https://curl.haxx.se/windows/) and give it another go. – JNevill Feb 19 '19 at 14:12
  • 2
    is `curl` in your path? from cmdline do `where curl` I suggest you rather give full path to curl in your batch file, if it is not in `C:\windows\system32` Like `c:\some dir\curl.exe` – Gerhard Feb 19 '19 at 14:12
  • If it is installed in your computer, try adding the full path to it, which is found if you run `where /r "C:" curl.exe` in cmd. – double-beep Feb 19 '19 at 14:13
  • 2
    @double-beep `where /r` is not recommended here, the idea of running `where` is to see if the file is in path, if you recurse it, it will search for the file and will not solve the issue. technically, `curl.exe` defaults in `c:\windows\system32` which should be in path and should return by `where curl` – Gerhard Feb 19 '19 at 14:18
  • @gerhard-barnard It works when I run it directly from terminal, so it should be installed. I'll try with full path, tnx. – ShadowWolf01 Feb 19 '19 at 14:26
  • ok, let me know then, if it is in `c:\windows\system32` just run it as `curl.exe` another thing, is it needed to run `start batch.bat`? you should just be able to run `batch.bat` and lastly, confirm your batchfile is not actually named `curl.bat` or `curl.cmd` – Gerhard Feb 19 '19 at 14:32
  • Can you perhaps expalin why you're changing directory to `%AppData%`? Is `curl.exe` inside that directory?, If not, is that the intended location for the output file, `.`? – Compo Feb 19 '19 at 14:50
  • @gerhard-barnard It is in the said path, but even with the full path given, it gives the same error. I also tried adding ```where curl``` in that script and it doesn't find it either. Nope, if I remove 'start' it doesn't execute it. The batchfile I'm, using to test it is called ```test.bat``` – ShadowWolf01 Feb 19 '19 at 14:55
  • @Compo yes that's correct, that's the location where I want to output file – ShadowWolf01 Feb 19 '19 at 14:56
  • 1
    you might want to take a look at this: https://stackoverflow.com/questions/28143160/how-can-i-download-a-file-with-batch-file-without-using-any-external-tools/28143180#28143180 – npocmaka Feb 19 '19 at 14:58
  • 1
    Is there a reason why you cannot forget about changing directory, and just use `-o "%AppData%\."`, then? – Compo Feb 19 '19 at 15:02
  • @Compo thanks for the idea, I'll use that. But ```curl``` still doesn't work nonetheless – ShadowWolf01 Feb 19 '19 at 16:24
  • @ShadowWolf01, I was merely simplifying your command, not providing an answer. Is this the command you say doesn't work? `exec("curl -o \"%AppData:\=\\%\\. – Compo Feb 19 '19 at 17:00
  • @Compo After modifying it by your suggestion, I'm using ```exec("curl -o %AppData%\.");``` now – ShadowWolf01 Feb 19 '19 at 21:08
  • 1
    @npocmaka I tried using ```bitsadmin``` from the list you sent, and it works! Thank you very much – ShadowWolf01 Feb 20 '19 at 13:26

1 Answers1

1

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. Thanks to everyone who helped!