Search code examples
phpphp4

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

Solution

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