简体   繁体   English

如何在PHP中将文件从URL保存到Sourceforge Web服务器?

[英]How to save file from URL to Sourceforge web server in PHP?

I have a simple web hosting, on SourceForge. 我在SourceForge上有一个简单的虚拟主机。 All web hosting on SourceForge uses SFTP . SourceForge上的所有Web托管都使用SFTP

Now, I want to get a file on a other server (I have no administrative permission ; but, I can view it in web browser normally. Such as: http://data.example.com/videos/fun.mp4 .) to my web hosting on SourceForge (The hosting structure is: /home/project-web/my-own-project/htdocs/my-folder ). 现在,我想在另一台服务器上获取一个文件(我没有管理权限 ;但是,我可以在网络浏览器中正常查看它。例如: http://data.example.com/videos/fun.mp4http://data.example.com/videos/fun.mp4 。)到我在SourceForge上的虚拟主机(主机结构是: /home/project-web/my-own-project/htdocs/my-folder )。

I have read these answers on many questions, like: 我已经在许多问题上阅读了这些答案,例如:

I have tried with all the answers. 我试过所有的答案。 It displayed the succeed message; 它显示了成功消息; but, there is no file on my server. 但是,我的服务器上没有文件。


Then, I have tried with changing CHMOD to 777 (I understand that it is damage; so, I tried for testing only). 然后,我尝试将CHMOD更改为777 (我知道它是损坏;因此,我只尝试进行测试)。 And, there is no file too; 并且,也没有文件; in other answer, there is an empty file. 在其他答案中,有一个空文件。

I have use phpinfo(); 我用过phpinfo(); for checking the cURL , it is (always) enabled. 为了检查cURL ,它(始终)被启用。


Can you tell me what is wrong in my doing list. 你能告诉我在做清单上有什么问题吗? And, please show me: "How to upload file from URL to SourceForge web hosting, by PHP?". 并且,请告诉我:“如何通过PHP将文件从URL上传到SourceForge虚拟主机?”。


My question is not duplicated with existed questions on Stack Overflow. 我的问题与Stack Overflow上存在的问题没有重复
I have used a 20 KB file for testing; 我使用了20 KB的文件进行测试; but, the uploaded file on my server is empty. 但是,我服务器上的上传文件是空的。

Updated as per Use-Policy: https://sourceforge.net/p/forge/documentation/Project%20Web%20and%20Developer%20Web/#outbound-connectivity 按使用政策更新https//sourceforge.net/p/forge/documentation/Project%20Web%20and%20Developer%20Web/#outbound-connectivity

Due to past abuse, outbound access to other hosts from the web servers is strictly prohibited, and is blocked by firewall rules. 由于过去的滥用行为,严禁从Web服务器对其他主机进行出站访问,并被防火墙规则阻止。 This includes access to other SourceForge.net hosts; 这包括访问其他SourceForge.net主机; the only exceptions to this policy are access to our project database servers and authorized outgoing emails. 此策略的唯一例外是访问我们的项目数据库服务器和授权的外发电子邮件。 SCM server access is not available from the project web servers. 项目Web服务器无法访问SCM服务器。 Any content needed from other SourceForge.net hosts should be retrieved and stored as a file or database entry that may then be accessed from scripts on the project web servers. 应检索其他SourceForge.net主机所需的任何内容,并将其存储为文件或数据库条目,然后可以从项目Web服务器上的脚本访问这些条目。 While our PHP install (and other languages) include support for things like LDAP, IMAP and Postgres, we do not permit inbound or outbound connectivity using these protocols. 虽然我们的PHP安装(和其他语言)包括对LDAP,IMAP和Postgres等内容的支持,但我们不允许使用这些协议进行入站或出站连接。 These components are included so third-party code may be kept stock when being deployed on SourceForge.net web servers; 包含这些组件,因此在SourceForge.net Web服务器上部署第三方代码时可以保留库存; and to support the testing of project code which requires these functions. 并支持测试需要这些功能的项目代码。

So the ability to connect to external hosts is blocked by a firewall and is not possible. 因此,防火墙阻止了连接外部主机的能力,但这是不可能的。


Please post your phpinfo() output in your question. 请在您的问题中发布您的phpinfo()输出。 Specifically the system details (top section), core, curl, sockets, and standard section values (leave out any confidential info like database passwords/connections) . 特别是系统详细信息(顶部),核心,卷曲,套接字和标准部分值(省略任何机密信息,如数据库密码/连接)

There are lots of things that could prevent it from saving, user permissions (on the executable), server configuration, suPHP (sandboxed user), chrooted web directory, php configuration, etc. We would need to see the actual code you used to diagnose your specific issue . 有很多东西可以阻止它保存,用户权限(在可执行文件上),服务器配置,suPHP(沙盒用户),chroot web目录,php配置等。我们需要看到你用来诊断的实际代码你的specific issue

As for something to try, use this code. 至于要尝试的东西,请使用此代码。

<?php

if (file_put_contents('/home/project-web/my-own-project/htdocs/my-folder/test.txt', 'Hello World!')) {
   echo 'File Saved Successfully!';
}

if test.txt exists and says Hello World! 如果test.txt存在并说Hello World! when you download and open it from SFTP, try the below. 当您从SFTP下载并打开它时,请尝试以下操作。

Also most SFTP clients need to be refreshed after new content has been added to the server. 在将新内容添加到服务器之后,还需要刷新大多数SFTP客户端。 So please ensure you have refreshed your SFTP client. 因此,请确保您已刷新SFTP客户端。


<?php

umask(0000); //this sets everything this script interacts with as everyone writable/executable 
//(same as chmod(0777))

$output_path = '/home/project-web/my-own-project/htdocs/my-folder';
$out_file = 'fun.mp4';
$source = file_get_contents('http://data.example.com/videos/fun.mp4');
if (empty($source)) {
  die('No Content Received');
}
if (!@mkdir($output_path, 0777, true) && !is_dir($output_path)){
   die('Unable to create destination directory!');
}
if (file_put_contents($output_path . '/' . $out_file, $source)) {
   echo 'File Saved Successfully!';
}

I tested the above on my webserver with a 5 MB MP4 (specifically http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 - first google result for an mp4 download) and it worked fine. 我在我的网络服务器上测试了上面的5 MB MP4(特别是http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4下载mp4的第一个google结果),它运行正常。

This would be a last ditch effort to get it to work. 这将是让它发挥作用的最后努力。

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

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