Create folder on FTP server in directory where PHP

2020-07-26 07:41发布

问题:

I use Current PHP version: 7.1.4 for Ionic2 application back-end.

I'm trying to create folder on my ftp server in the same directory, where directory.php located itself.

After link to http://site/php/directory.php echo:

Successfully created images

it creates images folder in user directory:

home/user/public_html/folder/php/uploads/

My directory.php located in php folder:

home/user/public_html/folder/php/uploads/

I want create images directory in exist uploads folder:

home/user/public_html/folder/php/uploads/images

uploads folder allows to create folder manually with FTP client tool

So what I have to do, to create it in uploads directory:

<?php
$ftp_server = "ftp_address";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "ftp_username", "ftp_password");

$dir = "images";

if (ftp_mkdir($ftp_conn, $dir))
  {
  echo "Successfully created $dir";
  }
else
  {
  echo "Error while creating $dir";
  }
ftp_close($ftp_conn);
?> 

回答1:

If you are creating a directory on the web server, the directory is local in a context of the PHP script. So do not use FTP functions, use functions for a local file system. Namely mkdir.

<?
$dir = "images";
if (mkdir($dir))
{
    echo "Successfully created $dir";
}
else
{
    echo "Error while creating $dir";
}

Most web servers execute PHP script with its actual location as a working directory. So a relative path like image will resolve relatively to a path of your PHP script.