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

时间:2012-08-14 03:24:56

标签: php mysql image file-upload

我有一个允许文件上传的表单部分的以下脚本,完整脚本在这里:Issue with image upload php: undefined index。它工作正常,如果没有选择文件则停止脚本,并在表单完全填充时上传(仅限登录用户)

到目前为止,我想要做的是没有运气,但是仅将上传类型限制为pdf和jpeg。

欢迎任何建议

    <?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;
    }
    ?>      

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

这将为你做到这一点......

    <?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';
    } 
    ?>