dpx49470 2010-09-20 10:32
浏览 42
已采纳

为什么这个php文件上传验证脚本不起作用?

Dear friends, this is a script which simply upload file and insert filename into database, why is this not working ? It's just upload the file and send filename to db even after validation . Please help

<?php

//file validation starts
//split filename into array and substract full stop from the last part
$tmp = explode('.', $_FILES['photo']['name']);
$fileext= $tmp[count($tmp)-1];

//read the extension of the file that was uploaded
$allowedexts = array("png");
if(in_array($fileext, $allowedexts)){
    return true;
}else{
    $form_error= "Upload file was not supported<br />";
    header('Location: apply.php?form_error=' .urlencode($form_error));
}


//file validation ends

//upload dir for pics
$uploaddir = './uploads/';


//upload file in folder
$uploadfile = $uploaddir. basename($_FILES['photo']['name']);


//insert filename in mysql db
$upload_filename = basename($_FILES['photo']['name']);



//upload the file now
    move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile);

// $photo value is goin to db
$photo = $upload_filename;
  • 写回答

4条回答 默认 最新

  • dqrzot2791 2010-09-20 10:42
    关注
    function send_error($error = 'Unknown error accured')
    {
        header('Location: apply.php?form_error=' .urlencode($error));
        exit; //!!!!!!
    }
    //file validation starts
    //split filename into array and substract full stop from the last part
    
    $fileext = end(explode('.', $_FILES['photo']['name'])); //Ricky Dang | end()
    
    //read the extension of the file that was uploaded
    $allowedexts = array("png");
    if(!in_array($fileext, $allowedexts))
    {
    }
    
    //upload dir for pics
    $uploaddir = './uploads/';
    if(!is_dir($uploaddir))
    {
        send_error("Upload Directory Error");
    }    
    
    //upload file in folder
    $uploadfile = $uploaddir. basename($_FILES['photo']['name']);
    
    if(!file_exists($uploadfile ))
    {
        send_error("File already exists!");
    }
    
    //insert filename in mysql db
    $upload_filename = basename($_FILES['photo']['name']);
    
    //upload the file now
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile))
    {
        send_error('Upload Failed, cannot move file!');
    }
    
    // $photo value is goin to db
    $photo = $upload_filename;
    

    This is a cleared up version to yours, give that a go and see if you get any errors

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵