How do I deal with the following error so that my script doesn't stop working when the exception occurs:
NotReadableException in AbstractDecoder.php line 302: Image source not
readable
I've tried using the following ($file is the url of the image):
// Return false if error
try
{
$img = Image::make($file);
}
catch(NotReadableException $e)
{
return false;
}
This doesn't seem to catch the exception and return false. What else can I do?
You either need the full namespaced exception in the catch area or add the use statement for that exception at the top of the file
Add Intervention\Image\Exception\NotReadableException:
use Intervention\Image\Exception\NotReadableException;
try {
//
} catch(NotReadableException $e) {
//
}
Related
I am using slim to upload files like the following :
foreach ($files as $file) {
try {
$fileLog = $file->moveTo($location));
}
catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
}
return true;
the problem i that there is probably a runtimeexception when there is no space in the disk and the phph trying to save the file.
I am looking for a way to always catch any exception with saving file :
no sapce, broken file ...etc
for now the issue is I cannot catch the runtime exception
I am using PhpSpreadsheet for a project and am trying to use the exact method for setting column widths. It works fine until the true type font path is not set or cannot be found.
The code I have is:
if (EXACT_AUTOSIZE)
{
if (PHP_OS == 'WINNT')
{
\PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath("C:/Windows/Fonts/");
}
else
{
\PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath(__DIR__."/fonts/");
}
try {
\PhpOffice\PhpSpreadsheet\Shared\Font::setAutoSizeMethod(\PhpOffice\PhpSpreadsheet\Shared\Font::AUTOSIZE_METHOD_EXACT);
}
catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
die("Unable to use exact autosize.");
}
}
When executing the script I am getting the error: Uncaught exception 'PhpOffice\PhpSpreadsheet\Exception' with message 'Valid directory to TrueType Font files not specified'
If I change the code to throw the exception in the try block without the function call it is caught by the catch block.
Hi everyone! In my Laravel application I have a upload function for an Excel file. I got this code from the web and adjusted it to my application. The problem is, it doesn't catch a fatal error exception which is produced when the user submits a file, but hasn't selected a file. I don't understand why it is not being caught. I will add a part of my controller.
public function upload() {
$file = array('thefile' => Input::file('thefile'));
$rules = array('excel' => 'excel');
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
return Redirect::to('UploadExcelFile')->withInput()->withErrors($validator);
}
else {
try{
// FatalErrorException happens in this line!
if (Input::file('thefile')->isValid()) {
$destinationPath = 'uploads';
$fileName = Input::file('thefile')->getClientOriginalName();
Input::file('thefile')->move($destinationPath, $fileName);
Session::flash('success', 'Upload successfully');
$fileNameJSON = exec("python /path/to/script/ExcelToJSON3.py $fileName"); // This returns a full path name of the JSON file that is made...
if ($fileNameJSON == null){
return Redirect::to('/dashboard/input');
}
else {
// Getting the ID from the file that is uploaded
try {
$jsonDecode = json_decode(file_get_contents($fileNameJSON));
} catch (ErrorException $e){
return Redirect::to('/errorpage')->with(array('status'=> 'ErrorException'));
}
A lot of code for handling the data entered....
}catch (FatalErrorException $e){
return Redirect::to('/errorpage')->with(array('status'=> 'FatalErrorException'));
}
}
return true;
}
The error that is given:
FatalErrorException in UploadExcelFileController.php line 35:
Call to a member function isValid() on a non-object
So, I don't understand why this code doesn't handle the error exception and how I could fix this!
Unless you've imported the namespace that FatalErrorException is declared in with "use" you will need to scope the exception, like this:
catch (\Symfony\Component\Debug\Exception\FatalErrorException $e) {
Otherwise you're using whatever namespace your class is in and trying to catch an exception that is declared in that namespace. It looks like your ErrorException is similarly set up.
I'm not sure that the namespace above is the one your class is deriving from, I'm just guessing it is because you're using Laravel.
I am trying to catch the exception while uploading file in zend framework 1.
I denied permission to the folder and then ran the following code to catch the exception but it is not working.
public function uploadImage($postedFile,$destination) {
try {
$imageName = $this->getFileName($postedFile); //$postedFile is same as $_FILES
$upload = new Zend_File_Transfer();
foreach ($upload->getFileInfo($imageName) as $info) {
if ($info['name'] != '') {
$ext = pathinfo($info['name'], PATHINFO_EXTENSION);
$newName = md5(rand(1, 100).date('ymdhis') . $info['name']) . '.' . $ext;
$upload->addFilter('Rename', $destination."/".$newName);
if (!$upload->receive($info['name'])) {
return FALSE;
}
}
break;
}
return $newName;
} catch (Zend_File_Transfer_Exception $e) {
throw new Exception('I want to catch this');
}
}
error:
Warning:
move_uploaded_file(/var/www/html/glistonapp/application/../public/images/app_user_profile_picture/80d55d25c52ef4d74079cfa903288b77.png):
failed to open stream: Permission denied in /var/www/html/glistonapp/library/Zend/File/Transfer/Adapter/Http.php on line 189
Warning: move_uploaded_file(): Unable to move '/tmp/phpOtOLVv' to '/var/www/html/glistonapp/application/../public/images/app_user_profile_picture/80d55d25c52ef4d74079cfa903288b77.png' in /var/www/html/glistonapp/library/Zend/File/Transfer/Adapter/Http.php on line 189
It doesn't appear that the methods you are calling on the Zend_File_Transfer object will throw an exception.
Without a thrown exception, you won't be able to "catch" anything in your try block. Instead, you should check the return values of the functions you are calling to determine if there was a problem.
See the API reference which will tell you which methods throw exceptions:
http://framework.zend.com/apidoc/1.12/classes/Zend_File_Transfer_Adapter_Abstract.html
Your error message is not an exception, therefore it won't be caught by the try/catch block.
You should set the appropriate permissions on the destination directory for that error message.
I use imagick to create thumbnails of PDF files, but in some cases imagic returns a Fatal Error.
I am looking for a way to know when the fatal error occurs.
Something like this:
function MakeThumb($source) {
if($im = new imagick($source)) {
//Generate thumbnail
return($thumb);
} else {
return 'no_thumb.png'; // then we will not tray again later.
}
}
You could do something like this
function MakeThumb($source) {
try {
//throw exception if can't create file
if(!$im = new imagick($source) {
throw new Exception('Count not create thumb');
}
//ok if got here
return($thumb);
} catch (Exception $e) {
return 'no_thumb.png';
}
}
I haven't tested it,but by using Try Catch, you can make it work
http://php.net/manual/en/language.exceptions.php