array_map with unlink, using a wild card not working - php

Here's what I currently have:
$pic_path provides a path like so: path/to/img
$toid is a number, and like pic_path, no slash before or after.
$file_path = glob( $pic_path . "/" . $toid . ".*" );
array_map('unlink', $file_path);
The picture I am trying to delete is indeed in the specified path.
I've tried using:
$file_path = glob( $_SERVER['DOCUMENT_ROOT'] . "/" . $pic_path . "/" . $toid . ".*" );
array_map('unlink', $file_path);
Also tried:
$file_path = glob($pic_path . "/" . $toid . ".*");
array_walk($file_path, function ($file) {
unlink($file);
});
and I've also tried replacing the * wild card for the actual image extension.
Any idea?

Related

Why does the variable value get deleted when placed after DIRECTORY_SEPARATOR?

I'm trying to get this path:
C:\xampp\htdocs\site\folder\filename.jpg
By doing:
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\folder\
But it doesn't have the $filename.
I have also tried:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'folder' . DIRECTORY_SEPARATOR . $filename);
This returns:
C:\xampp\htdocs\site\files\folder\filename.jpg
So I tried adding ..\ to get away from the files folder:
$i = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . '..\folder' . DIRECTORY_SEPARATOR . $filename);
But that returns:
C:\xampp\htdocs\site\files\..\folder\filename.jpg
How can I get to the correct folder?
$filename = 'filename.jpg';
$i = new Imagick('C:\xampp\htdocs\site\folder' . DIRECTORY_SEPARATOR . $filename);
This should give the correct value

PHP - move_uploaded_file not working to copy file in same folder

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;
}

File exists always returning false

My image directory is \webroot\files\thumbs
I am trying to add file_exists condition. For that I tried bellow code
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'jpg';
$file_exists = file_exists($file);
It's always returning zero.
If I echo $file it's giving me output like this
c:\xampp\htdocs\myproject\files\thumbs\_61.jpg
You're missing the . before the extension. Update your $file definition as follows:
$file = WWW_ROOT .'files' . DS . 'thumbs' . DS .'_'.$password->collection_id.'.jpg';
// This was missing ---^
$file_exists = file_exists($file);

Recursive directory with mkdir not working

I am uploading multiple images within a single category and I would like to store each group of images in a unique directory within my 'images/' directory as follows:
'images/unique_category/image1.jpg'
I have the following code but it is not creating a directory. I suspect it has something to do with setting the recursive parameter as 'true' which I believe I have done. Am I using the mkdir function incorrectly?
Thank you!
$unique_directory = "../images/".$_POST['item_name'];
if (is_dir($unique_directory)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
echo "Stored in: " . $unique_directory."/".$_FILES["file"]["name"];
}
else
{
mkdir($unique_directory, 0777, true);
move_uploaded_file($_FILES["file"]["tmp_name"],
$unique_directory."/".$_FILES["file"]["name"]);
Here, give this a try. I tested it on my (hosted) server and it works. Yet, I tested it by placing the files in the root of it, and used images instead of ../images for the $unique_directory variable.
I also used the chmod command apart from the other function, because the other method did not work.
N.B.: If possible, try changing 0777 to 0755, because using 0777 is not the safest setting.
<?php
$filename = $_POST['item_name'];
$unique_directory = "../images";
if (!is_dir($unique_directory . '/' . $filename)){
mkdir($unique_directory . "/" . $filename);
chmod("$unique_directory" . "/" .$filename, 0777);
}
if (is_dir($unique_directory))
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "1) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], $unique_directory . "/" . $filename . "/" . $_FILES['file']['name']);
echo "2) Stored in: " . $unique_directory . "/" . $filename . "/" . $_FILES['file']['name'];
}
?>

Move file into a specific folder

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);
}
}

Categories