File exists always returning false - php

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

Related

How do I upload files on 2 folders at once in one method on the laravel?

I use laravel 5.6
I want to upload files on 2 folders at once. So if the image uploaded successfully in the product folder, I want the image uploaded in the thumbs folder as well
I try like this :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPath = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product';
if(!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
If the code run, it just success to upload in the product folder. It did not upload in the thumbs folder
There exist error like this :
message Unable to find file (/tmp/phpUSxbEJ).
exception Intervention\Image\Exception\NotReadableException
I try run this code :
public function uploadImage($file)
{
if($file) {
$fileName = str_random(40) . '.' . $file->guessClientExtension();
}
$destinationPathThumb = storage_path() . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . 'thumb';
if(!File::exists($destinationPathThumb)) {
File::makeDirectory($destinationPathThumb, 0755, true);
}
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
So I remove code to upload in product folder. I try it and it works. It success upload on the thumbs folder
So I think in one process, it only upload in one folder
Is there any other way to upload on 2 folders?
You first upload the temporary file which will be removed once you save it on your disk, thats why u can't reuse it, instead of reusing it, you fetch the saved image and do resizing on it and save it with a different name:
public function uploadImage($file)
{
...
$file->move($destinationPath, $fileName);
//$file here doesn't exist anymore, hence it can't be read
...
$uploadedFile = Storage::get($destinationPath . DIRECTORY_SEPARATOR . $filename);
$image_resize = Image::make($uploadedFile);
$image_resize->resize(300, 300);
$image_resize->save($destinationPathThumb . DIRECTORY_SEPARATOR . $fileName);
return $fileName;
}
I find a solution
I try like this :
$file = $file->move($destinationPath, $fileName);
It works

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

file_get_contents() cannot read PHP tags <?php ?>

I am using cakePHP and got stuck with this riddle.
Time ago i made function that inserts custom routes dynamically in file using file_get_contents() and string replace functions.
$filename = "301routes.php";
$path = SERVER_PUBLIC_PATH . 'app' . DS . 'config';
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);
this worked, but now i tried to put exactly the same function in other project and its working only when there is no PHP tag. Problem is with file_get_contents() function becouse it reads file well when content is:
// routes here
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
but when i change beginning like this
<?php
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
?>
file_get_contents() returns only part of file:
string(154) "'index_router', 'action'=>'redirect_301','/articles/category/en/test-old')); ?>"
Can someone help me with this?
To run php codes and assign to $file_data variable,
ob_start();
$file_data = file_get_contents($path . DS . $filename);
ob_end_flush();
Try: Create new file 301routes.php in config directory then read write permission of file.
$filename = "301routes.php";
$path = __DIR__;
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);

array_map with unlink, using a wild card not working

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?

Autoload files when use require function

There's a magic function to autoload classes (__autoload), I want to know if there a way to load a file without a class.
Something like this:
require ('example_file'); // Trigger __autoloadfiles function
function __autoloadfiles($filename)
{
$files = array(
ROOT . DS . 'library' . DS . $filename. '.php',
ROOT . DS . 'application' . DS . $filename . '.php',
ROOT . DS . 'application/otherfolder' . DS . $filename. '.php'
);
$file_exists = FALSE;
foreach($files as $file) {
if( file_exists( $file ) ) {
require_once $file;
$file_exists = TRUE;
break;
}
}
if(!$file_exists)
die("File not found.");
}
You can define own function for requiring:
function require_file($file) {
// your code
}
and then call it
require_file('file');
I guess that there is no way to overload require function.

Categories