ftp_rename is a built-in PHP function used to rename files or directories on an FTP server. Its basic syntax is as follows:
<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">ftp_rename</span></span><span> ( resource </span><span><span class="hljs-variable">$ftp_stream</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$from</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$to</span></span><span> )
</span></span>
$ftp_stream: The FTP connection resource, which must be established using functions like ftp_connect or ftp_ssl_connect.
$from: The path of the original file or directory to be renamed.
$to: The path for the new file or directory name, i.e., the name after renaming.
The function returns TRUE if the rename operation succeeds and FALSE if it fails.
First, use the ftp_connect function to connect to the FTP server. If the server requires authentication, log in using ftp_login.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$ftp_server</span></span><span> = </span><span><span class="hljs-string">"ftp.example.com"</span></span><span>;
</span><span><span class="hljs-variable">$ftp_username</span></span><span> = </span><span><span class="hljs-string">"username"</span></span><span>;
</span><span><span class="hljs-variable">$ftp_password</span></span><span> = </span><span><span class="hljs-string">"password"</span></span><span>;
<p></span>// Establish FTP connection<br>
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to FTP server");</p>
<p>// Log in to FTP server<br>
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);</p>
<p>if (!$login) {<br>
die("Login failed.");<br>
}<br>
?><br>
</span>
Use the ftp_rename function to rename a directory on the FTP server. For example, to rename /old_directory to /new_directory, you can use the following code:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$old_dir</span></span><span> = </span><span><span class="hljs-string">"/old_directory"</span></span><span>;
</span><span><span class="hljs-variable">$new_dir</span></span><span> = </span><span><span class="hljs-string">"/new_directory"</span></span><span>;
<p></span>// Rename directory<br>
if (ftp_rename($ftp_conn, $old_dir, $new_dir)) {<br>
echo "Directory renamed successfully!";<br>
} else {<br>
echo "Failed to rename directory!";<br>
}<br>
?><br>
</span>
After completing the rename operation, close the connection to the FTP server to free resources:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Close FTP connection</span></span><span>
</span><span><span class="hljs-title function_ invoke__">ftp_close</span></span><span>(</span><span><span class="hljs-variable">$ftp_conn</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
When using the ftp_rename function, pay attention to the following points:
Ensure that the FTP account has sufficient permissions to rename directories. Without the proper permissions, ftp_rename will fail.
The ftp_rename function does not check if the source directory exists. If the source directory is missing or the path is incorrect, the operation will fail. You can use ftp_chdir or ftp_nlist to verify the existence of the source directory.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">ftp_chdir</span></span><span>(</span><span><span class="hljs-variable">$ftp_conn</span></span><span>, </span><span><span class="hljs-variable">$old_dir</span></span><span>)) {
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">ftp_rename</span></span><span>(</span><span><span class="hljs-variable">$ftp_conn</span></span><span>, </span><span><span class="hljs-variable">$old_dir</span></span><span>, </span><span><span class="hljs-variable">$new_dir</span></span><span>)) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Directory renamed successfully!"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Failed to rename directory!"</span></span><span>;
}
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Source directory does not exist!"</span></span><span>;
}
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
The paths specified in ftp_rename are relative to the FTP root directory. Ensure that the paths are correct. For renaming items in the root directory, use the directory name directly; for subdirectories, include the full relative path.
Related Tags:
FTP