Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Delete files from FTP server

When we need to delete files which hosted on FTP server we need to get permissions to write. So to do that We need to connect to the FTP server.


1
2
3
4
<?php
$ftp_server = "Ftp_server_address_goes_ehre";
ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
?>


complete PHP code to delete file from FTP server.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// connect and login to FTP server
$ftp_server = "FTP_server_address_goes_here";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

$ftp_username = "ftp_username";
$ftp_userpass = "ftp_password";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
//provide the file name with the path starting from default root directory
$file = "public_html/files/file.txt";

// try to delete file
if (ftp_delete($ftp_conn, $file))
  {
  $msg = "$file deleted";
  }
else
  {
  $msg = "Could not delete $file";
  }
echo $msg;
// close connection
ftp_close($ftp_conn);
?>



Delete files using PHP

Delete uploaded file in PHP using unlink() method.


We can delete files by providing the path to the file in sever. Unlink command work only if  write permissions given for the file.otherwise unlink command will get failed.


//path starting from default root directory
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
$path = "photos/wallpaper.jpg"; 

if(unlink($path)){
$msg = "deleted"; 
}else{
$msg = "cannot delete"; 
}
echo $msg;
?>

Sometimes we will get warning If either the file is not exists or due to write permissions not enabled.
To ignore the warning we can suppress by adding @ symbol 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
//path starting from default root directory
$path = "photos/wallpaper.jpg"; 

if(@unlink($path)){
$msg = "deleted"; 
}else{
$msg = "cannot delete"; 
}
echo $msg;
?>


Upload Images to server using PHP

Simple example to upload images to server with PHP.

When we need to upload images/ files to server file manager(hosted in local server or hosted website) it is easy to create a PHP file using Dreamweaver. 
Using Dreamweaver
1. Go to File > New  and select PHP
2.Then add Following form anywhere between <body></body> tag.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table bgcolor="#009900" align="center" width="300" border="1" cellspacing="0" cellpadding="10">
 
    <td class="tableheading">Image</td>
    <td><input type="file" name="fileupload" accept="image/jpeg" required/></td>
  </tr>
  <tr height="40" class="regTableTag1">
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="PUSH" />
    </td>
  </tr>
 </table>
</form>