简体   繁体   English

使用php-pdf和jpeg进行图像上传文件类型验证

[英]Image upload file type validation using php - pdf and jpeg

I have the following script for part of a form which allows a file upload, full script is here: Issue with image upload php: undefined index . 我有一个允许文件上传的表单部分的以下脚本,完整脚本在这里: 图像上传问题php:undefined index It works ok in that it stops the script if there is no file selected and uploads when the form is fully populated (for logged in users only) 它工作正常,如果没有选择文件则停止脚本,并在完全填充表单时上传(仅限登录用户)

What I am trying to do with no luck so far, however is confine the upload type to pdf and jpeg only. 到目前为止,我想要做的是没有运气,但是仅将上传类型限制为pdf和jpeg。

Any suggestions welcome 欢迎任何建议

    <?php         
    if (is_uploaded_file ($_FILES ['image']['tmp_name'])) {
    if (move_uploaded_file($_FILES ['image']['tmp_name'], 
    "uploads/{$_FILES['image']['name']}")) { // Move the file over.
    echo '<p>The file has been uploaded!</p>';
    } else { // Couldn't move the file over.
    echo '<p><font color="red"> The thumbnail image could not be uploaded.</font></p>';
    $i = FALSE;
    }
    $i = $_FILES['image']['name'];
    } else {
    $i = FALSE;
    }
    ?>      

你应该阅读手册$ _FILE类型它会是这样的:

if ($_FILES['file']['type'] == 'image/jpeg')  && ($_FILES["file"]["type"] == "application/pdf")

This will do the trick for you... 这将为你做到这一点......

    <?php
    if(isset($_FILES['image'])&&(!empty($_FILES['image']))){  
    if (($_FILES['image']['type']== "image/jpeg")||($_FILES['image']['type'] == "application/pdf")){
$filename =$_FILES['image']['name'];
    $copy = copy($_FILES['image']['tmp_name'], "uploads/".$filename);
    if($copy){
    echo '<p>The file has been uploaded!</p>';
    }else{
    echo 'File is not a jpeg or pdf';
    }
    }else{
    echo '<p><font color="red"> The thumbnail image could not be uploaded.</font></p>';
    }
    }else{
    echo 'no image is selected';
    } 
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM