I have a very simple photo uploader which needs some bringing up to speed please
Firstly an echo appears when the page loads even tho there is nothing in the box?
if($_POST['upload']) {
if($_FILES['image']['name'] == "")
{
#there's no file name return an error
echo "\n<b>Please select a file to upload!\n</b>";
exit;
}
#we have a filename, continue
}
#directory to upload to
$uploads = '/hom开发者_如何学JAVAe/habbonow/public_html/other/quacked/photos';
$usruploads = 'photos';
#allowed file types
$type_array = array('image/gif','image/pjpeg','image/x-png');
if(!in_array($_FILES['image']['type'], $type_array))
{
#the type of the file is not in the list we want to allow
echo "\n<b>That file type is not allowed!\n</b>";
exit;
}
the page output shows the uploading box but also echos "That file type is not allowed!" even when i haven't clicked the button.
secondly what is the mime type for jpg please, as I have jpeg and pjpeg.
thanks, any help appreciated.
I would also suggest putting everything in the POST block, otherwise it will be evaluated when the page is loaded no matter what.
For mimetypes there is a method image_type_to_mime_type which lets you pass in a constant representing a given filetype and returns the proper mimetype for it, e.g.:
$type_array = array(image_type_to_mime_type(), image_type_to_mime_type(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_PNG), 'image/pjpeg');
(Since pjpeg doesn't have it's own constant we can just add it manually)
If you haven't submitted the form i think that the !inarray call could very likely return false because $_FILES['images'] wouldn't exist.
For this i would be tempted to put the whole lot in the first if statement:
if($_POST['upload']) {
if($_FILES['image']['name'] == "")
{
#there's no file name return an error
echo "\n<b>Please select a file to upload!\n</b>";
exit;
}
#we have a filename, continue
#directory to upload to
$uploads = '/home/habbonow/public_html/other/quacked/photos';
$usruploads = 'photos';
#allowed file types
$type_array = array('image/gif','image/pjpeg','image/x-png');
if(!in_array($_FILES['image']['type'], $type_array))
{
#the type of the file is not in the list we want to allow
echo "\n<b>That file type is not allowed!\n</b>";
exit;
}
}
精彩评论