File not readable in Laravel - php

Im trying to open a template with PHPExcel, in laravel.
$objPHPExcel = PHPExcel_IOFactory::load(URL::to_asset('myfile.xls'));
However, I get a File is not readable error.
When I do this...
if(is_readable(URL::to_asset('myfile.xls')){
echo "is readable";
} else { echo "nope";}
I get "nope" returned to me.
If i echo an image from this same asset directory, it displays fine.
<img src='URL::to_asset('img/test.png')'>
However if i do this....it also return "nope"
if(is_readable(URL::to_asset(img/test.png')){
echo "is readable";
} else { echo "nope";}
What is going on here?
Why if I visit the link in my browser the files display fine.
yet when I test them for readbility, they return false results??
This is Larave 3
Thanks

The PHP function is_readable() expects a system file as argument, so does PHPExcel_IOFactory::load(); you are passing a URL instead, which will always fail.
You need the local system path to the file and use that instead.

Related

file_exists returns false even when the path is right

I have seen some similar questions here, but no answer fit my needs.
I have a Wordpress and in the upload dir there is an image, I have the following url for the image: ../../uploads/2016/2/56c3620a9c8af.png I try to access the file from inside the child theme folder:
/home/mydomain/www/wp-content/themes/twentyfifteen_child
I would like to check if the file exists and afterwards unlink() the image, but the file_exists always returns false, even though I can echo the image.
The following simple function outputs the image but returns false.
function checkImageExist($url)
{
echo '<img src = "'.$url.'" /><br>'; //The image is rendered
clearstatcache();
if(file_exists($url))
{
echo 'Image exists '.$url;
}
else
{
echo 'Image does not exist';
}
}
And this is what I CAN'T UNDERSTAND.
What am I missing?
THERE WAS NO QUESTION AT ALL:
The script I was using to upload the files was duplicating the extension names, so 56c3620a9c8af.png was in fact 56c3620a9c8af.png.png this stupid mistake was the responsible of images appearing when called (as the browser was able to parse them even though the duplicated extension) but file_exists was not able to find them.
So everything was working and I was mistaken about the error source.
You are passing a $url when what you really need is a $path. You can get the path like this:
$path = wp_upload_dir(); //This returns an array with URL info
$path['path']. '/2016/2/56c3620a9c8af.png'

Best way to see if a view file (.ctp) exists in Cake PHP?

I am using CakePHP 3, and would like to change the behaviour of the handy PagesController which comes with the installation.
The current solution they use when trying to find and render a view file (.ctp) is using a try{} block, which is working well.
Actual code:
try {
$this->render(implode('/', $path));
} catch (MissingTemplateException $e) {
But in my case the most common situation will be that the .ctp file does not exist. (If it does not exist, it will go on with a default view and try fetch content from database, but it is not my problem here.)
In my modified version the most normal case will be that the MissingTemplaceException is thrown, which seem a bit overkill.
Why can I not simply check if the file exists?
Am I thinking right here? And if I am, how do I check for the file's existence?
After some fiddling around, I found the APP constant. This works:
$path = func_get_args();
$file = APP.'Template'.DS.'Pages'.DS.implode('/', $path).'.ctp';
if (file_exists($file))
{
// Render the file.
}
else
{
// Render some default file.
}
Why can I not simply check if the file exists?
I don't know why you can't. Just use file_exists()?
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

Use of move_uploaded_file() function issue

I have hosted my website to my server.The website is functioning well as same as the localhost.However,When i try to move uploaded files to the uploads folder,the move_uploaded_file() function is not working as it is working with the localhost.On using the mysql_error() function to find the error,it is not giving me any results.What is the problem with my code
code one is not giving me any results
code 1
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo mysql_error();
}
code two is outputing the else phrase
echo'cannot move file to folder';
code 2
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo 'cannot move file to folder ';
}
Make sure that the second parameter i.e destination where you are storing the uploaded file should be in string format.

Open and List files/folders in a zip archive in PHP

Well,
What I need is:
PHP script which can open and the echo out all top level files and folder included in a zip archive saved at location:
/submittions/projects/zip/file_name.zip
So, if a zip archive contains 3 folders and 2 files at top level where all of these three top level folders have sub folder in them as well, I should get result as:
folder_name_1
folder_name_2
folder_name_3
file_name_1
file_name_2
I tried searching here but still with nothing.
There is a tutorial for extracting but it doesn't tell clearly about listing each file:
http://net.tutsplus.com/articles/news/how-to-open-zip-files-with-php/
I tried zip_open($path_to_zip)
While using this function, I am getting output as 5. I believe it is an error number.
All those ZIP files are set to CHMOD 777
One more thing, on applying zip_open(), if I test if file is open or not, I am getting two different cases like this:
$zip = zip_open($path);
// CHECK ONE
if($zip) {
echo "Opened";
} else {
echo "Failed";
}
// CHECK TWO
if(is_resource($zip)) {
echo "Opened";
} else {
echo "Failed";
}
Check ONE returns Opened, check TWO returns Failed.
I just want the file and folder names in a zip archive and it would be great if you get me a simple and straightforward solution without any part of extraction and all, if its possible?
My server's PHP configuration is here: http://academicadda.com/info.php
Below is simple code where zip.zip is in same directory:
<?php
$zip = zip_open('zip.zip');
if(is_resource($zip)) {
echo "open";
} else {
echo "failed";
}
?>
OUTPUT: open
What should be code to read each top level file and folder? zip_read() ?
if($zip)returns true because its a positive value, in your case 5. And an error code 5 references to an "can't read" problem. I suggest you to check your path and permissions.
Thank you guys for addressing me.
It was path and permission problem.
Here is the solution I have finally got after a workaround:
<?php
$zip = zip_open('zip.zip');
if(is_resource($zip)) {
while (($zip_entry = zip_read($zip)))
{
echo $path = zip_entry_name($zip_entry);
}
} else {
echo "failed";
}
?>

how to check if .exe file exists in php

Hi there is there any way to check if .exe file exists on a given path or not.
I have installation of ImageMagic. I have a path of convert.exe of Image Magic. I need to check that in given path the convert.exe exists or not. I have implemented
$settingFileContent = file_get_contents($settingFilePath);
// print_r($settingFileContent);
$allPaths = unserialize(stripslashes($settingFileContent));
if (isset($allPaths['IMAGE_CONVERT_EXE'])) {
//cho $allPaths['IMAGE_CONVERT_EXE'];
if (file_exists($allPaths['IMAGE_CONVERT_EXE'])) {
$analysisResultObj->level = ENUM_SUCCESS;
} else {
$analysisResultObj->level = ENUM_ERROR;
$analysisResultObj->infoText = "Image Magic convert.ext has wrong path";
Logger::getLogger('Application')->error('Image Magic convert.ext has wrong path');
}
}
I can change the value of $allPaths['IMAGE_CONVERT_EXE'] in file. When I change to wrong value even in that condition it returns true.
Based on the documentation comment specifically about PHP on Windows I'm guessing (and let's be clear: everything in PHP is a guess) try this:
$file = 'd:/somfolder/imagemagic/convert.ext'
if(file_exists($file)) {
// should be false
}
Based on your actual code have you tried:
$file = $allPaths['IMAGE_CONVERT_EXE'];
if(file_exists($file)) {
// should be false
}
Looking at the documentation someone commented about having this same problem on Windows and being unable to return the correct result when concatenating string values. While you are not concatenating string values together its at least worth a shot to make sure there isn't something else strange going on.
To me it sounds like you're trying to get wether or not the Imagemagick extension exists. PHP provides ways for doing just that thus eliminating your extrapolated and insane approach all together.
<?php
echo extension_loaded('imagick');
?>
Additionally, you can get an idea of your installed extensions via
<?php
print_r(get_loaded_extensions());
?>

Categories