在PHP中使用shell_exec到ftp

时间:2021-06-08 00:05:03

I can only use shell_exec command to FTP a file to another server. This is due to the shared servers fastCGI timing out. So, no I can not use any other commands.

我只能使用shell_exec命令将文件FTP到另一台服务器。这是由于共享服务器fastCGI超时。所以,不,我不能使用任何其他命令。

How do I implement it? I test the script on my PC in a DOS window and it works, but on the server using code:

我该如何实现它?我在DOS窗口中测试我的PC上的脚本,它可以工作,但在服务器上使用代码:

$output = shell_exec('ftp -s:ftpscript.txt');
echo "<pre>$output</pre>";

I get the following error:

我收到以下错误:

ftp: s: unknown option

The batch file is:

批处理文件是:

open ftp.server.com
usernamex
passwordx
hash
cd incoming
binary
ls

Any ideas?

Background: I'm uploading files to Amazon AWS Glacier using PHP but large files (750Mb) time out and report a 500 Internal Server error due to the fastCGI (?) timing out. I need to find a way of uploading the large file using FTP.

背景:我使用PHP将文件上传到Amazon AWS Glacier,但是由于fastCGI(?)超时,大文件(750Mb)超时并报告500内部服务器错误。我需要找到一种使用FTP上传大文件的方法。

Andre

1 个解决方案

#1


0  

On *nix the use of the ftp:// wrapper should not cause timeouts, because time spent on stream operations should not count towards the time limit of the script. Try calling set_time_limit(0); at the top of the script to prevent the script from timing out.

在* nix上,使用ftp://包装器不应该导致超时,因为在流操作上花费的时间不应该计入脚本的时间限制。尝试调用set_time_limit(0);在脚本的顶部,以防止脚本超时。

Ref:

Note:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

set_time_limit()函数和配置指令max_execution_time仅影响脚本本身的执行时间。在确定脚本运行的最长时间时,不会包括在执行脚本之外发生的任何活动,例如使用system()的系统调用,流操作,数据库查询等。在测量时间真实的Windows上,情况并非如此。

It should also be noted that transferring 750MB files over FTP from a script called via HTTP is not a good solution, because the browser will need to be kept open while the transfer completes. What you should probably be doing is either using a cron job to perform the transfer, or using an HTTP request to start the transfer asynchronously.

还应该注意的是,通过HTTP从通过HTTP调用的脚本传输750MB文件不是一个好的解决方案,因为浏览器需要在传输完成时保持打开状态。您应该做的是使用cron作业执行传输,或者使用HTTP请求异步启动传输。

If this is not a viable solution for you, the problem you are running into with the shell_exec() approach is the difference between the Windows and *nix FTP programs.

如果这对您来说不是一个可行的解决方案,那么使用shell_exec()方法遇到的问题是Windows和* nix FTP程序之间的区别。

Does your server have a command line cURL binary? This would be a much better option if it is available, because the interface is standard regardless of platform. Also, you would not need to use scripts, you can accomplish pretty much anything with command line arguments.

您的服务器是否有命令行cURL二进制文件?如果它可用,这将是一个更好的选择,因为无论平台如何,接口都是标准的。此外,您不需要使用脚本,您可以使用命令行参数完成任何事情。

For example the above code could be condensed to this command:

例如,上面的代码可以压缩到这个命令:

$output = shell_exec('curl ftp://usernamex:passwordx@ftp.server.com/incoming/');
echo "<pre>$output</pre>";

This ignores the hash command from your script, but if you goal here is to get a directory list programmatically you should not require this option.

这会忽略脚本中的hash命令,但如果您的目标是以编程方式获取目录列表,则不应该使用此选项。

You can obtain a Windows cURL binary for testing on your local server here. I would probably recommend you use the file tagged like this (it's quite a way down the page):

您可以在此处获取Windows cURL二进制文件以在本地服务器上进行测试。我可能会建议你使用这样标记的文件(它在页面上相当不错):

Win32   7.27.0  binary  SSL SSH Dirk Paehl

Full documentation for the use of the cURL command line binary can be found here.

可在此处找到有关使用cURL命令行二进制文件的完整文档。

#1


0  

On *nix the use of the ftp:// wrapper should not cause timeouts, because time spent on stream operations should not count towards the time limit of the script. Try calling set_time_limit(0); at the top of the script to prevent the script from timing out.

在* nix上,使用ftp://包装器不应该导致超时,因为在流操作上花费的时间不应该计入脚本的时间限制。尝试调用set_time_limit(0);在脚本的顶部,以防止脚本超时。

Ref:

Note:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

set_time_limit()函数和配置指令max_execution_time仅影响脚本本身的执行时间。在确定脚本运行的最长时间时,不会包括在执行脚本之外发生的任何活动,例如使用system()的系统调用,流操作,数据库查询等。在测量时间真实的Windows上,情况并非如此。

It should also be noted that transferring 750MB files over FTP from a script called via HTTP is not a good solution, because the browser will need to be kept open while the transfer completes. What you should probably be doing is either using a cron job to perform the transfer, or using an HTTP request to start the transfer asynchronously.

还应该注意的是,通过HTTP从通过HTTP调用的脚本传输750MB文件不是一个好的解决方案,因为浏览器需要在传输完成时保持打开状态。您应该做的是使用cron作业执行传输,或者使用HTTP请求异步启动传输。

If this is not a viable solution for you, the problem you are running into with the shell_exec() approach is the difference between the Windows and *nix FTP programs.

如果这对您来说不是一个可行的解决方案,那么使用shell_exec()方法遇到的问题是Windows和* nix FTP程序之间的区别。

Does your server have a command line cURL binary? This would be a much better option if it is available, because the interface is standard regardless of platform. Also, you would not need to use scripts, you can accomplish pretty much anything with command line arguments.

您的服务器是否有命令行cURL二进制文件?如果它可用,这将是一个更好的选择,因为无论平台如何,接口都是标准的。此外,您不需要使用脚本,您可以使用命令行参数完成任何事情。

For example the above code could be condensed to this command:

例如,上面的代码可以压缩到这个命令:

$output = shell_exec('curl ftp://usernamex:passwordx@ftp.server.com/incoming/');
echo "<pre>$output</pre>";

This ignores the hash command from your script, but if you goal here is to get a directory list programmatically you should not require this option.

这会忽略脚本中的hash命令,但如果您的目标是以编程方式获取目录列表,则不应该使用此选项。

You can obtain a Windows cURL binary for testing on your local server here. I would probably recommend you use the file tagged like this (it's quite a way down the page):

您可以在此处获取Windows cURL二进制文件以在本地服务器上进行测试。我可能会建议你使用这样标记的文件(它在页面上相当不错):

Win32   7.27.0  binary  SSL SSH Dirk Paehl

Full documentation for the use of the cURL command line binary can be found here.

可在此处找到有关使用cURL命令行二进制文件的完整文档。