Seo Forum & AI

Coding & Programming => Programming Forum => PHP Forum => Topic started by: Walalayo on 09-20-2011, 04:33:02

Title: FTP file creation
Post by: Walalayo on 09-20-2011, 04:33:02
Hi
I need to create a file on the server using ftp via a PHP script.I can make a directory and connect etc ok, but I don't know how to create a file and put contents in etc. Thanks very much
Title: Re: FTP file creation
Post by: portodon on 02-28-2017, 10:10:13
<?php$file = 'somefile.txt';$remote_file = 'readme.txt';// set up basic connection$conn_id = ftp_connect($ftp_server);// login with username and password$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);// upload a fileif (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { echo "successfully uploaded $file\n";} else { echo "There was a problem while uploading $file\n";}// close the connectionftp_close($conn_id);?>
Title: Re: FTP file creation
Post by: Lishmalinyjames on 05-28-2021, 11:52:18
Open Control Panel.
Click on System and Security.
Click on Administrative Tools.
Double-click the Internet Information Services (IIS) Manager shortcut.
On the "Connections" pane, right-click Sites, and select the Add FTP Site option.
Title: Re: FTP file creation
Post by: saravanan28 on 06-07-2021, 10:13:01
View the 'FTP overview' article to log into your server using an FTP client.
Make sure your client is configured to show hidden files.
Once you're logged in, make sure you're in the correct directory to create the file. ...
Right click and choose 'Create new file'.
Title: Re: FTP file creation
Post by: john2020 on 07-20-2023, 04:01:47
To create a file on the server using FTP via a PHP script, you can use the `ftp_put` function. Here's an example:

```php
<?php

// Server details
$server = "example.com";
$username = "your_username";
$password = "your_password";

// File details
$remoteFile = "/path/to/file.txt";
$localFile = "/path/to/local/file.txt";

// Create an FTP connection
$conn = ftp_connect($server);
if (!$conn) {
    die("Failed to connect to FTP server.");
}

// Login to the FTP server
if (!ftp_login($conn, $username, $password)) {
    die("FTP login failed.");
}

// Upload a file
if (ftp_put($conn, $remoteFile, $localFile, FTP_ASCII)) {
    echo "File uploaded successfully.";
} else {
    echo "Failed to upload file.";
}

// Close the connection
ftp_close($conn);

?>
```

Make sure to replace "example.com" with your server's hostname, "your_username" and "your_password" with your FTP credentials, and "/path/to/file.txt" and "/path/to/local/file.txt" with the appropriate paths for your file.

This code connects to the FTP server, logs in with the provided credentials, and then uses the `ftp_put` function to upload the local file to the remote server. The `FTP_ASCII` parameter specifies the transfer mode as ASCII; you can change it to `FTP_BINARY` if you need to transfer binary files.

Once you run this script, it will either display "File uploaded successfully" or "Failed to upload file" based on the success of the operation.
Title: Re: FTP file creation
Post by: deepika@2876 on 11-26-2025, 03:24:16
Use ftp_fput() in PHP, write your content to a temp file locally, then upload it to the server to create the file.