简体   繁体   中英

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. 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);

move_uploaded_file() doesn't accept URLs for either parameter. 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. you can use realpath(__FILE__) to determine the absolute path to a file.

如果由于不知道绝对路径而不能使用绝对路径,请使用PHP的realpath()找出它是什么,然后使用它。

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:

$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.

请注意,由于您将上传的文件放在httpdocs因此可以上传php文件并执行任意代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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