How to Execute PHP with Extension File.Php.Jpg

Why filename.php.jpg will work as a PHP file?

Apache controls what file extensions can and cannot execute PHP. This can be controlled on a server-level, or a per-site level (such as with .htaccess).

By default, a .jpg extension should not allow PHP execution. Perhaps the filename was really index.jpg.php and you have misread. However, in the event that the filename is really index.php.jpg, you'll need to look into all possible locations and lock-down your configuration to only allow .php extensions to execute PHP.

how to execute jpg or other extensions like php

You can't execute JPEG files per se, but you can have them be a PHP script that generates JPEG data to use in e.g. a <img> tag.

You need to set the handler for the file to application/x-httpd-php, output a content type of image/jpeg in the script, and, most importantly, output JPEG data. Might want to consider naming it <something>.jpg.php so you don't have to do the first one though, since that's server configuration.

PHP: When I save JPG image the file extension is always missing

Your code $key = basename($img,'.jpg').PHP_EOL; has a PHP_EOL at the end, saying that the line ends here, "discarding" anything after it when assembling your filename.

Change $key = basename($img,'.jpg').PHP_EOL; to $key = basename($img,'.jpg'); to have your file extension appended.

How to stop a file named filename.php.jpg from uploading

Take a look at the finfo extension, this allows you to determine the true file type as it sniffs the file type at the OS level.

http://php.net/manual/en/function.finfo-file.php

As finfo is an extension it will need to be installed and enabled.

http://php.net/manual/en/fileinfo.installation.php

Example

 $path = $_FILES[$key]['tmp_name'],$uploaddir.'/'.$name;
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$whitelist = array('image/jpg');

if (in_array(finfo_file($finfo, $path), $whitelist) && move_uploaded_file($path))
{
chmod($uploaddir.'/'.$name, 0644);
}


Related Topics



Leave a reply



Submit