I have a website with a gallery page where I can add, delete and edit photos and categories of photos.
Now according to my code (I had taken this code from the sitepoint article by Mayank Gandhi called Build an automated php gallery system in minutes) for the file, upload.php, I can upload only certain types of photos.
I am enclosing the part of the code which says so.
// initialization
$result_final = "";
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');
According to the code, if I try to upload a file which contains an extension different from what is mentioned above, I will get a message, File is not a photo. (refer part of code below).
if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
$result_final .= "File ".($counter+1)." is not a photo!<br />";
I am able to upload jpg files but when I tried to upload a gif or a png file, I was getting the message, File 1 is not a photo.
Now I can understand that the code does not contain type gif. But it does contain the extension, PNG. Why is it that I am still not able to upload it? What do I do to enable upload of all file types?
Thank you so much. I changed the code at the part where it mentioned about saving the image as well as the thumnail. I tried uploading a png image and it worked. I also added the line,
'image/gif' => 'gif',
to the list of known photo types and now I am able to upload gif images as well.
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/gif' => 'gif',
'image/x-png' => 'png');
What happens if you change image/x-png to image/png
I still get the message, File is not a photo.
I changed it back to image/x-png and now I am getting an error like this.
[B]
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/jppp/public_html/upload.php on line 98
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/jppp/public_html/upload.php on line 98
Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib error in /home/jppp/public_html/upload.php on line 147
Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/jppp/public_html/upload.php on line 147
File 1 Added Successfully!
[/B]
You shouldn’t be using “type” from _FILES to determine if it is an image or not.
logic_earth, I am a newbie in this matter. What should I be doing instead? Is it possible to rectify things by making a few changes to my code?
Normally, when one has an admin file to enable uploading of photos by the client, does everybody code in such a way so as to enable uploading of all types of photos or only certain types? What do the pros do?