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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!