I'm working on implementing the web site that consits of php, mysql, smarty, and am facing an issue of not being able to upload a mp4 file to the database at the registration screen which I am able to upload with jpg file.
When I click the upload button there it shows the % of uploading progressin at the lower left hand coner of the screen upto around 99% and then it says at the end "file format is unknown" that's from the php coding below on the client with white screen on the back, and it doesn't say any error message at the server.
My question is if you can tell from the coding below something wrong with case "1" which is for mp4, and case "2" is for jpg which is working fine. Or is it that some other coding that are related to the uploading function might be causing this problem? Or is it those kind of issue that it would be solved if I could install ffmpeg on the appache server? I'd appreciate if you could help me out.
Code:
function Main($path, $width, $height, $dst_file, $header = false) {
if(!isset($path)) {
return array(0, "Image path is not set.");
}
if(!file_exists($path)) {
return array(0, "The file can not be found in the specified path.");
}
// Set the size of the image
if($width) $this->imgMaxWidth = $width;
if($height) $this->imgMaxHeight = $height;
$size = @GetimageSize($path);
$re_size = $size;
//Aspect ratio fixed processing
if($this->imgMaxWidth != 0) {
$tmp_w = $size[0] / $this->imgMaxWidth;
}
if($this->imgMaxHeight != 0) {
$tmp_h = $size[1] / $this->imgMaxHeight;
}
if($tmp_w > 1 || $tmp_h > 1) {
if($this->imgMaxHeight == 0) {
if($tmp_w > 1) {
$re_size[0] = $this->imgMaxWidth;
$re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
}
} else {
if($tmp_w > $tmp_h) {
$re_size[0] = $this->imgMaxWidth;
$re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
} else {
$re_size[1] = $this->imgMaxHeight;
$re_size[0] = $size[0] * $this->imgMaxHeight / $size[1];
}
}
}
$imagecreate = function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate";
$imageresize = function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized";
switch($size[2]) {
case "1":
if ($header) {
header("Content-Type: video/mp4");
$dst_im = copy($path, $dst_file);
return "";
} else {
$dst_file = $dst_file . ".mp4";
$dst_im = copy($path, $dst_file);
}
unlink($dst_im);
break;
case "2":
$src_im = imageCreateFromJpeg($path);
$dst_im = $imagecreate($re_size[0], $re_size[1]);
$imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
if ($header) {
header("Content-Type: image/jpeg");
imageJpeg($dst_im);
return "";
} else {
$dst_file = $dst_file . ".jpg";
if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
copy($path, $dst_file);
} else {
imageJpeg($dst_im, $dst_file);
}
}
imagedestroy($src_im);
imagedestroy($dst_im);
break;
default:
return array(
0,
"file format is unknown"
);
}
return array(
1,
$dst_file
);
getimagesize()
only works on images. Its behavior on video files is undefined, it may give a width and height but it will probably just fail. Even if it does work, $size[2]
will not be "1" because that would mean it's a GIF image.
I would use mime_content_type()
to determine what type of file was uploaded. This will return a mimetype for the uploaded file, which means that you need to have multiple cases to deal with image files:
$mimetype = mime_content_type($path);
switch ($mimetype) {
case "video/mp4":
// what is currently your case "1"
break;
case "image/jpeg":
case "image/pjpeg":
case "image/png":
case "image/gif":
// what is currently your case "2"
break;
default:
return array(
0,
"file format is unknown"
);
}
This function is part of the Fileinfo extension, which you may need to install if it's not already enabled on your server.