FTP Delete All Folders in Target Directory - php

What i am trying to do is to delete all files in my target servers folder, the folder all the files are in is: "/public_html/" this directory contains all the target files, i don't want to delete this folder as it must remain the intact, just everything inside.
function ftpDelete($conn, $directory) {
echo "<pre><b>FTP Files on Server:</b>\n";
$filelist = ftp_nlist($conn, $directory);
foreach($filelist as $file) {
// Do not show "." or ".."
if ($file != "." && $file != "..") {
ftp_delete($conn, $directory);
echo $file . "\n";
}
}
echo "</pre>";
}
// Run delete functions ...
ftpDelete($conn, "/public_html/");
// Files out that is still on the server ...
FTP Folders on Server:
/public_html/vendor
/public_html/stats
/public_html/icon
/public_html/images
This code so far will delete all files that is the "public_html" directory, but not any folders, i know from reading the folders need to be empty first, i'm not sure the best way to handle these folders, i didn't see a command that would delete the target folders and it's contents, any help would be appreciated.

You need to append the filename after the directory:
ftp_delete($conn, "$directory/$file");

Related

Why do scandir, opendir and glob all NOT return my directory contents

I'm currently in the process of creating an application to generate and manage project names based on predefined themes. This application features very basic cloud saving functionality. It's super simple and designed to work without a database by saving the generated save data on files on a server.
In order for the program to download all the saved files I need to list all the saved files in a folder on the server. However, I can't seem to get the expected response from my server. I've tried 3 different ways to list all the files, and NONE of them return any files, which seems very odd to me.
$dir = "WordPress_SecureMode_01/Bubba/";
echo pathinfo($dir, PATHINFO_DIRNAME);
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
$files = scandir('WordPress_SecureMode_01/Bubba/');
foreach($files as $file){
echo $file;
echo pathinfo($file, PATHINFO_FILENAME);
}
$entries = glob('WordPress_SecureMode_01/Bubba/*.txt');
foreach($entries as $entry){
echo $entry;
}
As you can see I'm now using three different methods of retrieving the files. opendir, scandir and glob. All their findings are echoed and thus retrieved by my application. However, the only data my application receives is the output of the pathinfo method at the top of the script. So, the communication between client and server is working fine, but all the options for scanning directory files aren't.
Does anyone have an idea as to why this behaviour is ocurring?
You either want to use an absolute path, if the files are in the same directory:
$dir = __DIR__;
Or a relative path, if they are in the same directory:
$dir = "./";
Here in your code $dir is not a directory and that's the reason the code isn't moving forward.
Please check the path,whether it is dir or not by simply executing
$dir = "WordPress_SecureMode_01/Bubba/";
if (is_dir($dir)) {
echo 'Yes';
}
else{
echo 'No';
}
If it gives you Yes it means its not about path and if it returns you No then please change your path to that folder.

PHP DirectoryIterator and .DS_Store File

I have a local XAMP install on my Mac. I have this code:
foreach (new DirectoryIterator('test') as $fileInfo) {
if ($fileInfo->isDot() && $fileInfo->getBasename() !== '.DS_Store') {
continue;
}
$xmlFile = "test/" . $fileInfo->getFilename() . "/content.xml";
if (file_exists($xmlFile)) {
$xml = simplexml_load_file($xmlFile);
foreach ($xml->infos as $infos) {
echo "<li><a href='#tab_3' data-toggle='tab'>$infos->bezeichnung</a></li>";
}
}
}
The problem is that I get an error that says that the file .DS_Store cannot be opened. I thought my code filtered out the .DS_Store directory. It's clear that this file cannot be opened. It is a mac specific folder.
Your filter is saying "if it is a dot file and it is not .DS_Store, skip it" which is the opposite of what I believe you want. Try this:
if ($fileInfo->isDot() || $fileInfo->getBasename() === '.DS_Store') {
continue;
}
Which says "if it is a dot file (. or ..) or it is .DS_Store, skip it."
.DS_Store is a file, not a folder.
Your code is inappropriately assuming that anything returned by the directory iterator will be a folder and looking for a content.xml file inside it; this fails for regular files. .DS_Store happens to be a file, but the same issue would arise with any other file which happened to be in this directory.
Use the isdir method of DirectoryIterator to check whether the current item is a directory before trying to open files within it. Once you do this, you won't need a special case for .DS_Store.

Move uploaded file function in php is not working

Following is my piece of code which moves file to particular directory.
foreach($files as $dir_file) //Getting Path of XML file
{
echo "\nDir Files: ".$dir_file."\n";
if($dir_file != "." && $dir_file != "..")
{
if(strpos($dir_file,'.xml'))
{
echo "\nXML file found\n";
$xmlPath=$path."/".$dir_file;
// return ReadXML($xmlPath);
}
if(strpos($dir_file,'.JPG'))
{
echo "\n FOund \n";
$ret=move_uploaded_file($dir_file, 'upload/'.$dir_file));
echo "\n retunr values: ".$ret;
}
}
I have checked all permission and it is 777. but move upload file function is not moving my file to particular directory. it is also not returning anything. What i am doing wrong?
If you are to move files from one directory to another, you should use copy() function.
This is the example code:
$source = "YOUR_SOURCE_DIRECTORY/";
$destination = "YOUR_DESTINATION_DIRECTORY/";
foreach ($files as $file) {
if (in_array($file, array(".","..")))
continue;
//If file is copied successfully then mark it for deletion
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
//If you want to delete the copied files then include these lines
//Delete all successfully copied files
foreach($delete as $file) {
unlink($file);
}
move_uploaded_file
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.
(Emphasis mine.) You can use move_uploaded_file only to move files which have been uploaded, where "uploaded" means "PHP has received the file in the current request and the file is currently in the $_FILES super global". Any other file is not movable by move_uploaded_file. This is exactly on purpose, move_uploaded_file is supposed to protect you from yourself and make sure you're only moving files uploaded by the user.
If you want to move any other file which has not just been uploaded via HTTP POST, use copy or rename.

PHP, Ajax - access all files and folders on server and create file manager

I want to access all files and folders in a specific directory on server (for eg. "home"). Is there a way to do it? or i just need to say everything in database?
Also i want to create a kind of File Manager that manages all the files and folders in a specific directory on server (for eg. "home") like on windows we have file explorer.
Please help me...
Thank you for any help
Regards
try this simple php snippet and customize it your own way:
<?php
$directory = "./";
//Directory is set;
$dir = opendir($directory);
//Opened the directory;
while($file = readdir($dir)){
//Loops through all the files/directories in our directory;
if($file!="." && $file != ".."){
if(is_dir($file)) echo '<strong>'.$file.'</strong><br>';
else echo $file."<br/>\n";
}
}
?>
it'll output all the files & folders(not the subfolder files) in the current/provided dir.

PHP: moving directories with contents?

I'm having an array $bundle that stores filenames and directory names.
I'm running through the array with a foreach loop and I want to move them inside of another directory. I'm using the rename method therefore and it works pretty fine with JUST FILES.
However directories with other files in there don't respond to the rename() method.
$folder = 'files';
foreach ($bundle as $value) {
$ext = pathinfo($value, PATHINFO_EXTENSION);
if ($ext != "") { //if $value has no suffix it's a fil
rename(PATH . '/' .$value, $folder . '/' . $value);
}
if ($ext == "") { // it's a folder/directory
//rename doesn't work for directories with contents
//what method should i use here???
}
}
I know the pathinfo() method is not the best way to find out if it's a directory or not, however for my little project it's fine. I just need to know how I can move any directory with all it's contents into the "files" folder.
Thank you for your help.
You will have to get all the filenames in that directory using glob or scandir. Then you would have to loop through them with the rename and move them.
Your other option, if you host allows it, is to use shell_exec and do the mv for linux or copy / xcopy for windows command and move them that way. If you choose the exec route, make sure to secure the input etc to prevent any bad stuff from happening.

Categories