PHP:使用PHP将文件保存到不同的根目录?
好的,当我通过 move_uploaded_file() 使用 PHP 保存上传的文件时,我无法使用绝对 URL,我必须使用相对 URL。 我的网站有 2 个根目录,一个用于 http 端,一个用于 https 端:分别是 httpdocs 和 httpsdocs。 那么,如果我的脚本位于 https 端,如何将文件保存到 http 端的位置?
谢谢!
更新 好吧,看来我使用了错误的绝对路径约定,我这样做是这样的:
$dir = 'https://www.mydomain.com/masonic_images/';
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
OK when I save uploaded files with PHP via move_uploaded_file() I cannot use an absolute URL I have to use a relative one. My site has 2 root directories one for the http side and one for the https side: httpdocs and httpsdocs respectively. So if my script is on the https side how can I save the file to a location on the http side?
Thanks!
UPDATE
OK so it seems like I am using the wrong absolute path convention I am doing it like this:
$dir = 'https://www.mydomain.com/masonic_images/';
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
move_uploaded_file()
不接受任一参数的 URL。 目标是文件系统上的绝对路径。正如 @apphacker 建议的那样。 您可以使用
realpath(__FILE__)
来确定文件的绝对路径。move_uploaded_file()
doesn't accept URLs for either parameter. The destination is an absolute path on your filesystem.As @apphacker suggested. you can use
realpath(__FILE__)
to determine the absolute path to a file.如果因为不知道绝对路径是什么而无法使用绝对路径,请使用 PHP 的 realpath() 找出它是什么,然后使用它。
If you cannot use the absolute path because you don't know what the absolute path is, use PHP's realpath() to figure out what it is and then use it.
httpdocs 和 httpsdocs 目录是否都位于同一父文件夹中? 如果是这样,只需使用 move_uploaded_file 中第二个参数的相对路径即可将文件放置在另一个根目录中。
例如:
此代码假设上传脚本位于 httpsdocs 根目录,并且您要将文件保存到 httpdocs 目录中。
Are the httpdocs and httpsdocs directories both located in the same parent folder? If so, just use a relative path for the second parameter in move_uploaded_file to place the file in the other root directory.
For example:
This code assumes that the uploading script is located in the httpsdocs root directory, and that you want to save the file into the httpdocs directory.
请注意,由于您将上传的文件放入
httpdocs
中,因此可以上传 php 文件并执行任意代码。Note that since you put uploaded files inside
httpdocs
it could be possible to upload a php file and execute arbitrary code.