0

I am having trouble making remote directory using ftp.

I am getting error as Warning: ftp_mkdir(): Can't create directory: No such file or directory

I am trying this way.

$connection = ftp_connect($hostname) or die('Couldn\'t connect to ftp server'); 
$login = ftp_login($connection, $username, $password) or die('Couldn\'t log_in to ftp server');
ftp_pasv($connection, true);
if ($login){echo 'Connected Successfully.';}else{echo 'Cannot connect.';}

$dir = '2014/04/09';



if(!@ftp_chdir($connection, '/public_html/images/'.$dir)){
    $ftp_mkdir = ftp_mkdir($connection, '/public_html/images/'.$dir);
    if($ftp_mkdir){
        echo 'Remote directory created successfully';
    }
    else{
        echo 'Error creating remote directory';
    }
}
else{
        echo 'Remote directory exist';
}

Please see and suggest any possible way to do this.

Thanks.

  • Not sure what OS you are using but on Windows you can't have files with a "/" character in the name. – Scott Apr 09 '14 at 10:08
  • @Scott I am on windows7 and using linux hosting. –  Apr 09 '14 at 10:08
  • Yeah I don't think you are allowed that character in your file name. try changing `$dir = '2014/04/09';` to `$dir = '2014-04-09';` – Scott Apr 09 '14 at 10:10
  • @Scott that worked I get directory as images/2014-04-09, But the thing is that I want to make directory like images/2014/04/09/image.png, How can I do that. –  Apr 09 '14 at 10:13
  • Just for reference... http://stackoverflow.com/questions/9847288/is-it-possible-to-use-in-a-filename – Scott Apr 09 '14 at 10:50

2 Answers2

1

Had a similar problem and fixed by creating one folder at a time. I was not able to make ftp_chdir create the entire path on a single command.

So to create this:

$dir = '2014/04/09';

I had to create first:

$dir = '/2014/';

Then:

$dir = '/2014/04/';

At last:

$dir = '/2014/04/09/';

And then submit your file '/2014/04/09/image.jpg'.

Worked like a charm.

tomDev
  • 5,620
  • 5
  • 29
  • 39
0

In Windows * . " / \ [ ] : ; | = , are reserved characters and are not allowed to be used in your file names.

For Linux I think it's just / and \0

The / character is used to traverse file directories in Linux hence why it is forbidden to use as a file name.

For more info on illegal file names: http://en.wikipedia.org/wiki/Filename#Reserved%5Fcharacters%5Fand%5Fwords

You will have to substitute your / symbol with something else such as -

In replay to your comment: To make a directory like images/2014/04/09/image.png

You just need to create your folders on your server. So images/2014/04/09/image.png is a file called image.png which is inside the folder 09 which is inside the folder 04 which is inside the folder 2014 which is inside the folder images

Hope that makes scene?

Scott
  • 481
  • 4
  • 19
  • You said You just need to create your folders on your server? do i have to manually create the folders or can i create it using ftp. –  Apr 09 '14 at 10:30
  • You can do either... But by the looks of things you are following some naming convention with dates so you might want to write yourself a PHP script to handle this. So the script would get todays date, then check to see if there is already a file for this year, then month, then day, if no file found on these create a file. – Scott Apr 09 '14 at 10:41
  • You mean to say I have to create one after another these directories. Like create year directory first then month directory then date directory then put file. –  Apr 09 '14 at 10:44
  • Yes, to get a directory like this `images/2014/04/09/image.png` as you can't use the `/` character for file names. – Scott Apr 09 '14 at 10:50
  • Ok I think I have to make these directories like one after another as there is naming restriction. Thanks for taking time to explain. –  Apr 09 '14 at 10:55