i am trying to download a zip file from server and save it. i get the following error.
the project is in cakePHP
Downloading /server/biruhxml20140925.zip ...
Warning Error: ftp_get(): Transfer complete. in [(pathprefix)/app/Console/Command/Task/ImportUtilityTask.php, line 214]
//server/biruhxml20140925.zip could not be downloaded to (pathprefix)/files/downloaded_files/bild/biruhxml20140925.zip
biruhxml20140925.zip could not be downloaded as the file is not there yet.
this is the function which makes the call.
public function downloadFTPFile ($remoteFile, $localFile) {
$connection = $this->ftpConnection;
ftp_pasv($this->ftpConnection, true);
$this->out(__('Downloading %s ... ', $remoteFile));
try {
if (ftp_get($connection, $localFile, $remoteFile, FTP_BINARY)) {
$this->out(__('Saved %s', $localFile));
return true;
} else {
$this->out(__('%s could not be downloaded to %s', $remoteFile, $localFile));
return false;
}
} catch (Exception $e) {
#unlink($localFile);
$this->out($e->getMessage());
}
$this->nl();
return false;
}
can anyone suggest a work around to get rid of the warning other then setting debug level 0 in core.php
Have you considered, based on the error message, that the file you try to download is not present on the server?
Your code doesn't do a check if the file is there, I would add that and handle that case accordingly.
Related
I am currently using this command to validate some PHP files.
$op=null; $ret=null; exec("php -l '$file' 2>&1",$op,$ret);
Unfortunately on the customer's shared hosting (linux) it fails with the line below obviously because some commands are disabled:
Warning: exec(): Unable to fork [php -l '/path_to_the_file.php' 2>&1] in /my_program.php on line 559
I want to avoid this Warning at all costs because as soon as I disable debugging, the host shows its 500 error page which completely kills the webpage (for some strange reason).
Try/Catch does not work at all.
try {
$op=null; $ret=null; exec("php -l '$file' 2>&1",$op,$ret);
if($ret != 0) {
throw new Exception("'$file' failed syntax check");
}
} catch(Exception $e) {
$this->addLog(LOG_ERR, 'syntax error', $e);
continue;
}
Any ideas how to avoid this Warning?
This is the only solution that worked for me.
I used a couple of functions to check if exec is available and enabled:
private function commandEnabled($comm) {
return is_callable($comm) && !($this->commandDisabled($comm));
}
private function commandDisabled($comm) {
return !(false === stripos(','.ini_get('disable_functions').',', ','.$comm.','));
}
if (!$this->commandEnabled('exec')) {
// I run my code here
}
UPDATE:
I just found out from this post https://stackoverflow.com/a/29268475/1806085
that you can also catch the error in PHP 7+ with this:
try {
some_undefined_function_or_method();
} catch (\Error $ex) { // Error is the base class for all internal PHP error exceptions.
var_dump($ex);
}
I'm trying to upload files through the PHP function move_uploaded_file and this is what I have so far:
if (move_uploaded_file($file["tmp_name"], iconv("UTF-8","big5",$target_file))) {
return true;
}
else {
echo "Not uploaded because of error #".$file["error"];
exit(0);
return false;
}
As for $target_file, it is the location I'd like to upload my files, which is currently stu_feedback/105502504/feedback_20180910.pdf.
Yet, the files are all failed to be uploaded, which obviously goes to the else part.
But when I wanted to echo the error message, it only shows 0.
I use Filezilla Client. I have tested on my localhost and it could upload files correctly.
Does anyone know what actually happened?
I see some problems on this part :
Add enctype to the form <form enctype="multipart/form-data">;
Be sure that the folder where you want to move files are permissions (chmod -R /folder_name 777)
Try to put in a try catch you code something like :
try{
if (move_uploaded_file($file["tmp_name"], iconv("UTF- 8","big5",$target_file))) {
return true;
}
else {
echo "Not uploaded because of error #".$file["error"];
//exit(0);
return false;
}
}catch (\Exception $e){
var_dump($e->getMessage());
die();
}
Good day,
I modified this file.
vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
This is the function I modified.
public function handleError($level, $message, $file = '', $line = 0, $context = [])
I made it send me an email. The email has the $message and the $file and the $line.
This is what the email said.
MESSAGE FILE vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php LINE 126
The message is blank, but it's running into an error in a file. I open the file.
public function delete($paths)
{
$paths = is_array($paths) ? $paths : func_get_args();
$success = true;
foreach ($paths as $path) {
try {
if (! #unlink($path)) { # the error is here
$success = false;
}
} catch (ErrorException $e) {
$success = false;
}
}
return $success;
}
I am confused. Why was the error not caught by the catch block? What type of error would not give a message to the error handler of laravel? Is there something I can do to get more information regarding the error?
This is our version of Laravel.
Laravel Framework version 5.1.45 (LTS)
First of all the question you have asked is misleading. What you are trying to do is add a custom error handler in laravel which you can achieve by following this URL .
The error you are facing might be caused due to file permission error.
And since there is the # sign before the function call unlink() any errors which are caused by it are suppressed.
Remove the # sign and if the file doesn't exists or if there is an error it would throw it and then catch block would catch it.
I have a task to identify the existence of the links. As a result I am using guzzle client to identify if the link is existence or not, i.e if the response header received is 200, then the link is existence else not.
Below is my code snippet
public function checkUrl($url) {
$result['isValid'] = false;
try {
$response = $this->client->get($url, ['verify', false]);
} catch (\Exception $ex) {
$result['isValid'] = false;
$result['message'] = 'Some error message';
return $result;
}
if ($response->getStatusCode() == Response::STATUS_CODE_200) {
$result['isValid'] = true;
}
$result['message'] = 'Success - ' . $response->getStatusCode();
$response->getBody()->close();
return $result;
}
where $this->client is initialized to GuzzleHttp\Client object once in the constructor
When I run my script, after some time it throws me the error as follows:
PHP Fatal error: Uncaught ErrorException: include(/project/vendor/zendframework/zend-view/src/Model/ConsoleModel.php): failed to open stream: Too many open files
And when I check the list of open files using the command lsof -p <process id> -n, I noticed that there lots of open files are as a result of guzzle request (i.e curl responses) and it seems to be the cause of this exception.
Is there any suggestion for the solution through which I can close those responses?
My following method uses an array of data $FDFData to create an FDF file:
public function writeFDFFile($FDFData) {
$fdf_file = time().'.fdf';
$fdf = $this->createFDF(PDF_FORM, $FDFData);
// write the file out
if($fp=fopen($fdf_file,'w')) {
fwrite($fp,$fdf,strlen($fdf));
} else {
throw new Exception('Unable to create file: '.$fdf_file);
}
fclose($fp);
return $fdf_file;
}
One of my scripts runs absolutely fine:
require_once('PDFPrinter.php');
try {
$printer = new PDFPrinter();
$FDFData = $printer->assembleFDFData(9);
$fdf_file = $printer->writeFDFFile($FDFData);
$printer->downloadFile($fdf_file);
}catch(Exception $e) {
echo $e->getMessage();
}
but I can't get my test code to run because I get a:
Unexpected PHP error [fopen(1315558352.fdf) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: Permission denied]
error thrown:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once(dirname(__FILE__) . '/../PDFPrinter.php');
class PDFPrinterTest extends UnitTestCase {
private $printer;
private $FDFData;
function setUp() {
$this->printer = new PDFPrinter();
$this->FDFData = $this->printer->assembleFDFData(5);
}
function testException() {
try {
$this->printer->writeFDFFile($this->FDFData);
$this-assertTrue(true);
} catch(Exception $e) {
$this->assertTrue(false);
}
}
}
Both scripts are run in directories with the correct permissions. I am running my test scripts through the browser as well, so it's not that I have a different environment.
I'm stuck as to how to proceed to find the issue really.
My directory structure:
HOME - PDFPrinter.php
|
-----tests - PDFPrinterTest.php
|
------simpletest - autorun.php
Any suggestions as to how I could find the issue?
Many thanks
Update
I have tried changing my test class so that the only test function in there is:
function testWrite() {
try {
$name = "testing.txt";
if($fp=fopen($name, 'w')) {
fwrite($fp, "Blah");
} else {
throw new Exception("Nope.");
}
$this->pass("All good");
} catch(Exception $e) {
$this->fail("Not good");
}
}
but the exception is still thrown with the warning.
Yet a very simple script run from the same directory works fine:
$name = "Test.txt";
if($fp=fopen($name, 'w')) {
fwrite($fp, "Working");
} else {
throw new Exception("Nope.");
}
fclose($fp);
that will actually create and write to the file.
Finally found the solution which was that the file name needed to be the full absolute address in order for it to work in both scripts for some reason. This was suggested in one of the answers for this SO question which I quote below:
Use fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','a+');
so for my code, I have used:
if($fp=fopen($_SERVER['DOCUMENT_ROOT'].$name, 'w')) {
fwrite($fp, "Working");
}
In your test
fwrite("Blah");
should be
fwrite($fp, "Blah");
I'm not sure what the problem in the original code is though.
failed to open stream: Permission denied
There is one important programmer's skill every developer ought to master.
Here it is:
To trust your eyes
If your PHP telling you that permission is denied - so it is. Just doble-check it. It is not a big deal yet noone can do it for you.