1

对于允许文件上传的表单的一部分,我有以下脚本,完整的脚本在这里:图像上传问题 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;
    }
    ?>      
4

2 回答 2

3

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

if ($_FILES['file']['type'] == 'image/jpeg')  && ($_FILES["file"]["type"] == "application/pdf")
于 2012-08-14T03:33:17.623 回答
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';
    } 
    ?>
于 2012-08-14T03:51:39.767 回答