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

报告相同问题?