Upload file php 5.3+ (no framework)

if( isset($_FILES) && count($_FILES)>0 ){ if( $_FILES["photo"]["error"]==0 ){ $name_arr = explode(".", $_FILES["photo"]["name"]); $name_file = strtotime(date("Y-m-d H:m:s")).".".$name_arr[1]; move_uploaded_file($_FILES['photo']['tmp_name'], "../images/galeria/".$name_file ); }else{ $msg = "Couldn't load the image, Error code: ".$_FILES["photo"]["error"]; } }

2 Responses

Hi, Cesar!
You do not check the file type
Do better so:
$mime_types=array(
'image/jpeg' => array('imageCreateFromJpeg', 'imageJpeg')
,'image/gif' => array('imageCreateFromGif', 'imageGif')
,'image/png' => array('imageCreateFromPng', 'imagePng')
);
$src_file = realpath($_FILES['attach']['tmp_name'][$key]);
@list($src_w, $src_h, $src_type) = array_values(getimagesize($src_file));
$src_type = image_type_to_mime_type($src_type);
if (empty($mime_types[$src_type])) $error = "Image format not supported!";
@geralt gwynbleidd The file's type is up to each user, and her needs. Another solution could be this:
$allowed_ext = array('gif','png' ,'jpg');
$filename = $_FILES['photo']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed_ext) ) {
echo 'File unsopported';
}

Thanks for your comment, regards

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.