Mallow php's avatar

Could not able to put the file in FTP Server

I'm trying to put the xml file in FTP Server using php and laravel.

I could not able to upload the file in the Server.

Here it's my code in php

    $fileName = 'test.xml';
    $filePath = public_path($fileName);
    $conn_id = ftp_connect(env('FTP_HOST'));
    ftp_login($conn_id, env('FTP_USERNAME'), env('FTP_PASSWORD'));
    ftp_put($conn_id, '/files/test/', $filePath, FTP_ASCII);

But it is showing error as

    ERROR:
    ftp_put(): Could not create file.

I have tried in Laravel also

     $fileName = 'test.xml';
     \Storage::disk('ftp')->put($fileName, fopen(public_path($fileName), 'r+'));

I have configured the driver in filesystems

'ftp' => [
    'driver' => 'ftp',
    'host' => '12.12.12.12', 
    'username' => `user`,
    'password' => 'password',

],

Expected : Need to put the xml file in ftp server which is in project public path.

0 likes
4 replies
Sinnbeck's avatar

Does it work if you do it from a FTP client? Filezilla etc.?

Mallow php's avatar

@resin I'm able to connect the FTP Server in FileZilla...But Can't able to put the file in server using the above code.

ftiersch's avatar

Can you only connect or are you able to put a file at exactly the location you are trying to?

Sounds to me like the connection data is correct but you don't have write permissions or the folder doesn't exist. Also in your code it looks like you are trying to overwrite a directory. I'm not sure if ftp_put recognizes, that you want to put the file IN that directory and tries to put it instead of that directory.

Sinnbeck's avatar

Try this (based on suggestion from @ftiersch )

ftp_put($conn_id, '/files/test/' . $fileName, $filePath, FTP_ASCII);

Please or to participate in this conversation.