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

时间:2022-04-26 17:27:46

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. 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)

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

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

2 个解决方案

#1


3  

You should read the manual $_FILE type It would be something like:

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

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

#2


0  

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

#1


3  

You should read the manual $_FILE type It would be something like:

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

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

#2


0  

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