php - Identifying a pdf file and upload it to folder -
i´m using php code upload images folder allow pdf files uploaded also, modified little code:
<?php $target_dir = "extra_images/"; $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); $textfiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image if(isset($_post["submit"])) { $check = getimagesize($_files["filetoupload"]["tmp_name"]); if($check !== false) { //echo "<div class=\"alert alert-success\" role=\"alert\"><strong><span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\"></span> correct image type.</strong></div>"; $uploadok = 1; } else { echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>file not image.</strong></div>"; $uploadok = 0; } } // check if file exists if (file_exists($target_file)) { echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>file exists.</strong></div>"; $uploadok = 0; } // check file size if ($_files["filetoupload"]["size"] > 3750000) { echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>your file large.</strong></div>"; $uploadok = 0; } // allow file formats if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif" && $textfiletype != "pdf" ) { echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>only jpg, jpeg, png, gif , pdf (for plan article) files allowed.</strong></div>"; $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) { echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>the file not uploaded.</strong></div>"; // if ok, try upload file } else { if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) { echo "<div class=\"alert alert-success\" role=\"alert\">the file <strong>". basename( $_files["filetoupload"]["name"]). "</strong> has been uploaded.</div><br>please copy filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_files["filetoupload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> , paste in empty image field above , save form."; } else { echo "<div class=\"alert alert-danger\" role=\"alert\">there error uploading file.</div>"; } } echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> back</button></p>"; ?>
i added bit:
&& $textfiletype != "pdf" , this: $textfiletype = pathinfo($target_file,pathinfo_extension);
but changes made not working, still returns "this not image"
message.
what part of code identifies filetype? $imagefiletype special variable php uses identify filetypes?
i´m confused this. can help?
the file type pdfs application/pdf
if want check extension.
however, while can check file extensions, that's not reliable way of identifying whether file pdf or not (it's easy change file extension file, creating huge security hole).
while there's nothing in php getimagesize()
pdfs, can still check mime type step in process so:
if (!empty($_files['filetoupload']['tmp_name'])) { $finfo = finfo_open(fileinfo_mime_type); $mime = finfo_file($finfo, $_files['filetoupload']['tmp_name']); if ($mime != 'application/pdf') { echo 'this not pdf file!'; exit(); }
Comments
Post a Comment