How to trigger download failed error in php - php

On my site, i allow users to download videos, images (in chunks though), however, i want to make it in such a way that if a url parameter is not met, my download script should halt and cause download failed.
I did this:
<?php if(!$condition){
trigger_error('Fatal error occured', E_USER_ERROR);
}?>
The problem is that, the file downloaded( although, currupted), when i checked into the downloaded file in a notepad, i saw my fatal error ("Fatal error occured").
How do i stop the file from downloading at all?

Just put
exit();
inside the if, after you've triggered the error. This will halt the script execution so that any headers, content etc associated with the file does not get output.
Optionally you could also set an appropriate HTTP status code for the response.

Related

require file doesn't exist until refresh

I have a require line in my php script.
require_once('../main.inc.php');
every once in a while, when I load a page, I get a message 'failed to open stream. no such file or directory'
But when I hit refresh, the page loads just fine.
What might be happening that the script can't seem to find the file until I simply refresh?
I am really hitting the error at this point before I use the require/include
if(!file_exists('../main.inc.php')) die('Fatal error
try
include('../main.inc.php');

Handling corrupt/truncated files with PHP gd-png

I'm working on a script that has to work on a large set of images and I need a graceful way to handle corrupt/truncated images in the set.
Currently my script works for all valid images but crashes when it runs into a truncated file. It must've sneaked into the folders but I want to handle this case nevertheless.
Its a *.png.tmp file and when feeding it to imagecreatefrompng(), it causes a fatal error and stops my script.
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data...
It is recognized as PNG by both getimagesize() and exif_imagetype().
Since I can't handle it as an exception, is there a way to check if the file is valid without crashing my script and without relying on extensions in the filename?
I can live with skipping the image, but I don't know how to recognize it as a problem image before hitting the fatal error.
Did you try to suppress error by using # operator, and check if image is actually created?
foreach ($imagePaths as $imagePath)
{
$image = #imagecreatefrompng($imagePath);
if (!$image)
{
//maybe delete bad image?
unlink($imagePath);
continue;//do nothing in this iteration anymore
}
//do your magic here
}

PHP ftp_delete() generates warning "Command okay"

I can not find any answer to this as most problems revolve around a file not existing or a delete process not working.
I have an FTP device where I generate a file with an PHP script. After that, I try to FTP in, get the file and after that, delete it.
This all works fine, I can connect, get the file and save it locally and then delete it. Except for one thing, the ftp_delete() function results in a warning.
PHP gives me the following, when executing the script:
A PHP Error was encountered
Severity: Warning
Message: ftp_delete(): Command okay
I looked up the error code, it means it was successful. And it was because the file is deleted on the FTP device.
So why does this generate an PHP error?
Cheers.
The RFC 959 (FTP specification) mandates that on a successful completion of the DELE command, the server should respond with 250 status code.
The PHP FTP implementation is very strict, yielding a warning on any other code, even if it indicates a success (2xx class).
Your server probably uses some other 2xx code, like a generic 200 Command okay.

PHP copy() throwing error

I'm processing a large number of zips and can't track down an error.
The problem is that after the import script runs for a while it suddenly stops with an error at a random zip file. If I try to re-run the script for only that single file, it works (so I exclude that the zip is faulty).
The process is like this: Open zip archive, read content, copy images from the zip, close archive.
I have two questions:
1. Why doesn't PHP simply return false instead of throwing the following error:
PHP Error[2]: copy(zip:///path/to/file/319759.zip#nwh1131_1.jpg): failed to open stream: operation failed
Any ideas what the cause can be? Maybe something with the filesystem? (Server is running on Debian 7 wheezy)
Thanks

Cloudinary: Error in sending request to server-couldn't open file

In cloudinary php i have the required files and set my config too but when i try to upload a picture from php file
\Cloudinary\Uploader::upload("1.png");
i get error
( ! ) Fatal error: Uncaught exception 'Exception' with message 'Error in sending request to server - couldn't open file in C:\wamp\www\demo\src\Uploader.php on line 200
Exception: Error in sending request to server - couldn't open file "1.png" in C:\wamp\www\demo\src\Uploader.php on line 200
Call Stack
The uploader cannot find the file you are trying to upload. Try to put the 1.png file in the same directory as the php file and run:
\Cloudinary\Uploader::upload(realpath(dirname(__FILE__).'/1.png'));
Sounds like 1.png is hardcoded somewhere and your parameter is not taken into account. Try to do a project wide search for 1.png and see if you can replace that with a parameter.
I might as well provide an answer here because I had the same exact problem, contacted Cloudinary's support team, and their solution worked.
First, I have an images folder and keep all of them there.
\Cloudinary\Uploader::upload('images/image1.jpg');
That caused the same error you got. I then changed it to:
\Cloudinary\Uploader::upload("C:\\xampp\\htdocs\\test\\images\\image1.jpg");
That worked perfectly. However, I didn't want to use absolute paths, so that's why I contacted their support team. This is the code that works:
\Cloudinary\Uploader::upload( $_SERVER['DOCUMENT_ROOT'] . "/test/images/image1.jpg");
The test value in that string is the directory of my site, inside the htdocs folder.

Categories