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

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

Related

Laravel 8 - Upload storage path changed, how to show old files?

In laravel 8, I updated the storage path for image uploads. It was app/violations/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/', $filename);
$violation->violationStatement = $filename;
}
And it was updated to app/violations/speeding/Toyota/XXX-1234/11-June-2022/11-26 AM/filename.jpeg
if( $request->hasfile('violationStatement') )
{
$file = $request->file('violationStatement');
$extension = $file->getClientOriginalExtension();
$filename = 'violation-statement' . '-' . $violation->plateNumber . '-' . $violation->violationType . '.' . $extension;
$file->move('app/violations/' .
$violation->violationType . '/' .
$violation->carModel . '/' .
$violation->plateNumber . '/' .
Carbon::parse($violation->violationDateTime)->format('d-M-Y/H-i A'), $filename);
$violation->violationStatement = $filename;
}
The images are displayed in datatables, so obviously I'll have to change the image src to the new URL. When I do so, how can I also display the old images of the old directory?

How can I delete folder in laravel 5.3?

If I delete file like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
$result = File::delete($destinationPath . DIRECTORY_SEPARATOR . $filename);
It works
But if I delete folder like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
File::delete($destinationPath);
It does not work
Why delete folder does not work?
How can I solve this problem?
You are attempting to use the wrong method. You need to use deleteDirectory and not delete:
$destinationPath = public_path('img/products/'. $id);
File::deleteDirectory($destinationPath);
If you want to delete folder with files than use:-
use Illuminate\Support\Facades\Storage;
Storage::deleteDirectory($public_path);

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'];
}
?>

Autoload class issue

Trying to autoload classes from <root>/incl/classes folder.
The problem is, when I call some class for ex. ip like that
$ip= new ip();
$ip=$ip->get();
PHP gives error message Notice: Undefined variable: path . But in fact file already exists
I'm declaring all various paths at the top of page.
define("ds", DIRECTORY_SEPARATOR);
$path = array();
$path['root'] = $_SERVER['DOCUMENT_ROOT'];
$path['common'] = $path['root'] . ds . "common";
$path['design'] = $path['root'] . ds . "design";
$path['contents'] = $path['root'] . ds . "contents";
$path['content_images'] = $path['root'] . ds . "content" . ds . "images";
$path['design_images'] = $path['root'] . ds . "design" . ds . "images";
$path['blocks'] = $path['contents'] . ds . "blocks";
$path['includes'] = $path['root'] . ds . "incl";
$path['pages'] = $path['contents'] . ds . "pages";
$path['classes'] = $path['includes'] . ds . "classes";
$files = glob("common" . ds . "*.php");
array_unshift($files, $path['common'] . ds . "settings.php", $path['common'] . ds . "db.php");
foreach ($files as $filename)
require_once $filename;
//Auto loading classes
function __autoload($class_name) {
if (file_exists($path['classes'] . ds . $class_name . '.php')) {
require_once($path['classes']. ds . $class_name . '.php');
} else {
die($path['classes'] . ds . $class_name . '.php');
}
}
For testing purposes added die($path['classes'] . ds . $class_name . '.php'); line. It outputs \ip.php. I wonder, why it doesn't echo $path['classes'] even if I declared it before ?
It's a scoping issue. Your $path variable doesn't exist in your autoload function. It is a global variable, and you need to explicitly "invite" it:
function __autoload($class_name) {
global $path;
You should actually have gotten a notice about this.

Categories