[英]PHP: Saving files with PHP to different root directory?
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. 当我通过move_uploaded_file()用PHP保存上传的文件时,可以,我不能使用绝对URL,而必须使用相对URL。 My site has 2 root directories one for the http side and one for the https side: httpdocs and httpsdocs respectively.
我的网站有2个根目录,一个用于http端,一个用于https端:分别为httpdocs和httpsdocs。 So if my script is on the https side how can I save the file to a location on the http side?
因此,如果我的脚本位于https端,如何将文件保存到http端的位置?
Thanks! 谢谢!
UPDATE OK so it seems like I am using the wrong absolute path convention I am doing it like this: UPDATE OK,所以好像我使用了错误的绝对路径约定,我这样做是这样的:
$dir = 'https://www.mydomain.com/masonic_images/';
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
move_uploaded_file()
doesn't accept URLs for either parameter. move_uploaded_file()
不接受任何一个参数的URL。 The destination is an absolute path on your filesystem. 目标是文件系统上的绝对路径。
<?php
$dir = '/var/www/httpsdocs/'; // Adjust to your configuration
move_uploaded_file($_FILES['blue_image']['tmp_name'], $dir.$new_name);
As @ apphacker suggested. 如@ apphacker建议。 you can use
realpath(__FILE__)
to determine the absolute path to a file. 您可以使用
realpath(__FILE__)
确定文件的绝对路径。
如果由于不知道绝对路径而不能使用绝对路径,请使用PHP的realpath()找出它是什么,然后使用它。
Are the httpdocs and httpsdocs directories both located in the same parent folder? httpdocs和httpsdocs目录是否都位于同一父文件夹中? If so, just use a relative path for the second parameter in move_uploaded_file to place the file in the other root directory.
如果是这样,只需为move_uploaded_file中的第二个参数使用相对路径即可将该文件放置在另一个根目录中。
For example: 例如:
$uploaddir = '../httpdocs/';
$uploadfile = $uploaddir . basename($_FILES['myfile']['name']);
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. 此代码假定上传脚本位于httpsdocs根目录中,并且您要将文件保存到httpdocs目录中。
请注意,由于您将上传的文件放在httpdocs
因此可以上传php文件并执行任意代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.