Deleting images from a folder programmatically - drupal-7

i want to delete images from public:// sub folder pro grammatically. i have paths like this below.
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
i want to delete like this,
$path = 'http://localhost/Drupal/dbquery/sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
but it doesn't delete image. i try to do this from menu call back function. how do i do that?.

In Drupal 7 you can use the file_delete to delete the file and its DB records as well.

these codes work. i just changed the path to relative path like below.
$path = 'sites/default/files/take_snap/20140713110549.jpg';
$del = unlink($path);
Now it deletes file from public sub folder.

Related

Create folder in explicit directory AIR

Like the title says I want to create a folder in a specific directory with Adobe Air.
If I use static methods of File like File.userDirectory works fine but I need to give the choice to select the directory.
I am trying this:
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a directory");
function dirSelected(e:Event):void {
trace(file.nativePath);
file.resolvePath("new_folder");
file.createDirectory();
}
Nothing happens
"resolvePath: Creates a new File object with a path relative to this File object's path, based on the path parameter (a string)."
So:
var newDir:File = file.resolvePath("new_folder");
newDir.createDirectory();

Deleting content of the folder using VC++

I am using the following code to create a directory using the following code.
TCHAR dir_path[] = TEXT("C:\Users\Temp\abc");
if (!CreateDirectory(dir_path,NULL)) {
}
else
{
//directory already exists
}
I want to add the logic to delete all the content of the folder(Files and folders recursively if any are there) if the folder C:\Users\Temp\abc already exists.
Any idea how I can delete the content of the folder recursively?
Thanks in Advance,
Azeem
I am new to this site. Apologies in advance if I am mistaken anywhere.
Firstly find the entries in the directory:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx
See if they are directory or not:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740%28v=vs.85%29.aspx
and
http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117%28v=vs.85%29.aspx
If yes, change to it and call the function again http://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx.
Then delete it: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915%28v=vs.85%29.aspx

how to check if folder exists cakephp

Is there any instance to return bool? e.g. $folder->exists() => bool
$path = WEBROOT_DIR .'/files/'. $folder_name;
$folder = new Folder('../'.$path);
//DOESN'T WORK
debug($folder->exists());
//I can do it in this way
//returns bool
debug(file_exists($folder->path));
I want to check if directory exists delete it. but I want to use specifically cake method to check if folder exists
Try this:
$path = 'files' . DS . $folder_name;
$folder = new Folder($path);
if (!is_null($folder->path)) {
$folder->delete();
}
Cakephp Folder class does not have method exists like File class has.
You could check if the folder exists with standard php and then delete it.
if (file_exists('path/to/directory')) {
rmdir('path/to/directory', 777, true);
}
Basically it checks if the folder exists with the file_exists function and if the file does not exist then it will use rmdir function to create the folder.
Though I think you could use cakephp like this. This will delete the folder even if the folder does not exist because the Folder constructor will create the folder if the folder does not exist.
$folder = new Folder('path/to/directory', true, 777);
$folder->delete();

Search for a folder by its name in c# without specifying the path

I want to search a folder by its name. But I don't know the location of the folder.
Have to get the path of that specific folder.
How Can i do it?
You have to specify the directory to search for the folder using Directory.GetDirectories Method (String, String, SearchOption)
string[] directories = Directory.GetDirectories(#"c:\",
"*",
SearchOption.AllDirectories);
To get all drives from the computer, use DircotoryInfo.GetDrives and then search in all of them you can try:
DriveInfo[] allDrives = DriveInfo.GetDrives();
List<string> directoryList = new List<string>();
foreach (DriveInfo d in allDrives)
{
directoryList.AddRange(Directory.GetDirectories(d.Name , "*", SearchOption.AllDirectories));
}
// Only get subdirectories that begin with the letter "p."
string[] dirs = Directory.GetDirectories(#"c:\", "p*");
Console.WriteLine("The number of directories starting with p is {0}.",dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
Reference - Directory.GetDirectories Method (String, String)
If you dont know the drive then you need to search for all drives by changing the drives available on your system.
the only solution is using recursive search to surf all available folders and sub folders and also to jump access denied paths to have complete list of target result.

How to extract file name from the uploaded file/form with GAE?

The following data is uploaded to my GAE application -
How can I
get fields with files only
get filenames of the uploaded files?
get fields with files only
import cgi
values = self.request.POST.itervalues()
files = [v for v in values if isinstance(v, cgi.FieldStorage)]
get filenames of the uploaded files
filenames = [f.filename for f in files]
Edit: corrected snippet, now tested :)
Assuming the data is POSTed using a form, for #2, see Get original filename google app engine
For #1, you could iterate through the self.request.POST multidict and see anything that looks like a file. self.request.POST looks like this:
UnicodeMultiDict([(u'file_1', FieldStorage(u'file_1', u'filename_1')), (u'random_string_field', u'random_string_value')])
Hope that helps you out
-Sam
filename = self.request.POST['file'].filename
file_ext = self.request.POST['file'].type
OR
filename = self.request.params[<form element name with file>].filename

Resources