I have a question regarding file handle, i have:
Files:
"Mark, 123456, HTCOM.pdf"
"John, 409721, JESOA.pdf
Folders:
"Mark, 123456"
"Mark, 345212"
"Mark, 645352"
"John, 409721"
"John, 235212"
"John, 124554"
I need a routine to move files to correct folders.
In case above, i need to compare 1st and second value from file and folder. If are the same i move the file.
Complement to post:
I have this code, work right but i need to modify to check name and code to move files...
I'm confused to implement function...
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
foreach (glob($pathToFiles . DIRECTORY_SEPARATOR . '*.pdf') as $oldname)
{
if (is_dir($dir = $pathToDirs . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_FILENAME)))
{
$newname = $dir . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_BASENAME);
rename($oldname, $newname);
}
}
As a rough-draft and something that will only work with your specific case (or any other case following the same naming pattern), this should work:
<?php
// define a more convenient variable for the separator
define('DS', DIRECTORY_SEPARATOR);
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
// get a list of all .pdf files we're looking for
$files = glob($pathToFiles . DS . '*.pdf');
foreach ($files as $origPath) {
// get the name of the file from the current path and remove any trailing slashes
$file = trim(substr($origPath, strrpos($origPath, DS)), DS);
// get the folder-name from the filename, following the pattern "(Name, Number), word.pdf"
$folder = substr($file, 0, strrpos($file, ','));
// if a folder exists matching this file, move this file to that folder!
if (is_dir($pathToDirs . DS . $folder)) {
$newPath = $pathToDirs . DS . $folder . DS . $file;
rename($origPath, $newPath);
}
}
Related
In this case I am trying to add all files and subdirectories to my zip file.
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
// Adding all files
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
$filename = substr($file, strrpos($file, '/') + 1);
echo $filename . '<br>';
$zip->addFile($file, 'wordpress/wp-content/themes/' . $title . '/' . $filename);
}
// Adding all subdirectories
$directories = glob(CLIENT_PATH . '/*' , GLOB_ONLYDIR);
foreach (glob(CLIENT_PATH . '/*', GLOB_ONLYDIR) as $dir) {
$zip->addEmptyDir('wordpress/wp-content/themes/' . $title . '/' . $dir);
}
$zip->close();
Adding all files works perfectly well but adding all subdirectories won't work as expected.
This is how my unzipped file looks:
The subdirectory of my main directory is called assets and it also includes some more subdirectories and they also have some subdirectories. But as you see in the image above, assets includes just nothing. And also, I don't understand why it starts with home/pomcanys/ etc. instead of assets.
How can I fix this? Any help would be appreciated!
To automatically include files from the sub-directories use the -r parameter for recursiveness:
zip -r foo.zip *
I have a csv file with image name and Prefix for image and I want to add prefix in that image name and rename it and move it to another directory.
<?php
$fullpath = '/var/www/html/john/Source/01-00228.jpg';
$additional = '3D_Perception_';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $additional
. $info['filename']
. '.' . $info['extension'];
echo $fullpath ;
}
?>
Here Image file is store in Source Directory I want to rename it with some prefix and move it other directory like Destination
Please help me out to find solution for this.
<?php
$source_directory = '/var/www/html/john/Source/';
$dest_directory = '/var/www/html/john/Source/'; // Assuming you'll change it
$filename = '01-00228.jpg';
$prefix = '3D_Perception_';
$file = $source_directory . $filename;
$dest_file = $dest_directory . $prefix . $filename;
if (file_exists($file)) {
if (copy($file, $dest_file)) {
unlink($file); // Deleting source file.
var_dump(pathinfo($dest_file)); // Dump dest file infos, replace it with wathever you want to do.
} else {
// if copy doesn't work, do something.
}
}
?>
Try this, and take care about my comments. ;)
Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
I am having issue with RecursiveIteratorIterator moving to next item. I have a directory which has multiple files, and I am trying to go to each file, rename it, and do some other stuff on it. The RecursiveIteratorIterator picks the first file from the directory and renames it successfully. When I do $it->next(), it stays on same file, and tries to look for the file which was already renamed to something else. Below is my code sample. File permission are set to 777. Any ideas would be appreciated.
This only happens if I rename the file. If I remove renaming functionality, its moves to next item as expected.
//initialize iterator object
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, 0));
/* loop directly over the object */
while ($it->valid()) {
// check if value is a directory
if (!$it->isDot()) {
if (!is_writable($directory . '/' . $it->getSubPathName())) {
//direcotry not writable, throw error
} else {
// get current file info
$fileinfo = pathinfo($it->getSubPathName());
//get file extension
$ext = $fileinfo['extension'];
//if its a '.', move to next item
if (in_array($fileinfo['filename'], array(".", ".."))) {
$it->next();
}
// the current file name with complete path
$old_file = $directory . '/' . $it->getSubPathName();
throw new Exception('source file doesnot exist ' . $old_file, error::DOESNOTEXIST);
}
//generate new file name with path
$new_file = $directory . '/' . $it->getSubPath() . '/' . $filename . '.' . $ext;
//rename the file
rename($old_file, $new_file);
}
}
/* * * move to the next iteration ** */
$it->next();
}
i am trying to write a php upload script to upload files to different folders. My code works for direct path (something like 'path/to/directory') but not for dynamic path taken from runtime.
$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files'. $folder;
$uploadsDirectory = str_replace (" ", "", $toupload);
When i echo $uploadsDirectory it shows the exact path. Could any one help me what could be wrong in this?
You should check and see if the folder is created and if the script has permission to write files there.
What is the exact output of the upload script? (i.e. what errors does it throw?)
Try using dirname(__FILE__);
<?php
$directory_self = dirname(__FILE__);
$folder = "faruk"; //final folder
$toupload = $_SERVER['DOCUMENT_ROOT'] . $directory_self .'/files/'. $folder;
$uploadsDirectory = trim($toupload);
echo $uploadsDirectory."\n";
?>
Output on my laptop;
/home/test/Desktop/files/test
Try adding some debug stuff to see if the path you're generating actually exists and is writeable:
$directory_self = dirname($_SERVER['PHP_SELF']);
$folder = $_POST['folder_name']; //final folder
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '/files';
$uploadsDirectory = str_replace (" ", "", $uploaddir);
if (!is_dir($uploadDirectory)) {
die("$uploadDirectory is not a directory");
}
if (!is_writeable($uploadDirectory)) {
die("$uploaddir is not writeable");
}
$toupload = $uploadDirectory . $folder;
if (!is_writeable($toupload)) {
die("$toupload is not writeable");
}