php文件上传脚本无法正常工作

时间:2011-03-26 20:58:30

标签: php mysql forms file-upload

  

可能重复:
  File uploader script not working properly

我已创建此文件上传(仅适用于pdf和msword文档)脚本。但它没有用。检查了很多次,但不知道我哪里错了。请看看并帮助..

<?php
//Start session
session_start();

//Include database connection details
require_once('default.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PSWD);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_NAME);
if(!$db) {
    die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
    $str = @trim($str);
    if(get_magic_quotes_gpc()) {
        $str = stripslashes($str);
    }
    return mysql_real_escape_string($str);
}

$Fname = clean($_POST['Name']);
$Fpath = ($_FILES['file']['name']);
$Ftype = clean($_POST['type']);
$Fdesc = clean($_POST['Desc'];
$Fby = clean($_POST['By']);
$Ffor = clean($_POST['For']);

if($Fname == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($Fpath == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($Ftype == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($Fdesc == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($Fby == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}
if($Ffor == '') {
    $errmsg_arr[] = 'Login ID missing';
    $errflag = true;
}

function findexts ($fname) { 
    $fname = strtolower($fname) ; 
    $extensions = split("[/\\.]", $fname) ; 
    $n = count($extensions)-1; 
    $extensions = $extensions[$n]; 
    return $extensions;
}

$target = "Notice/";
$flag = 0;

if($_FILES['file']['error']==0){
    $flag = $flag+1;
    }
if($_FILES['file']['size']<1000000){
    $flag = $flag+1;
    }
if(($_FILES['file']['type']=="application/msword")||($_FILES['file']['type']=="application/pdf")){
    $flag = $flag+1;
    }   
if ($flag>2){
    $ext = findexts($_FILES['file']['name']);
    $rand1 = rand();
    $rand2 = $rand1.".";
    $target = $target.$rand2.$ext;
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target)){
        //$qry="INSERT INTO PALMSIS.Notice(NoticeID,Name,Date,Timestamp,Type,Descr,By,For) VALUES('$rand1','$Fname',NOW(),NOW(),'$Ftype','$Fdesc','$Fby','$Ffor')";
        //$result=@mysql_query($qry);
        echo "teah";
        }
    else {
        echo "failure";
        }   

    }
else {

    echo "Failure";

    }   

//If there are input validations, redirect back to the registration form
if($errflag) {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: ../novarif.php");
    exit();
}

&GT;

0 个答案:

没有答案