文件上传器脚本无法正常工作 [英] File uploader script not working properly

查看:137
本文介绍了文件上传器脚本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHP和MySql创建文件上传脚本。但这个脚本显示我的err.php页面(应该在有任何输入验证错误时执行)。我试图调整它,但没有机会。以下是我的php脚本

I am trying to create a file upload script with PHP and MySql. But this sript is showing me the err.php page (which should get executed when there is any input validation error).I've tried tweaking it but no chance. following is my php script

    <?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 clean($str) {

        $str = @trim($str);

        if(get_magic_quotes_gpc()) {

            $str = stripslashes($str);

        }

        return mysql_real_escape_string($str);

    }





    //Function to sanitize values received from the form. Prevents SQL injection



    $name = clean($_POST['Name']);

    $toupld = ($_POST['file']);

    $source = clean($_POST['type']);

    $Desc = clean($_POST['Desc']);

    $By = clean($_POST['By']);

    $For = clean($_POST['For']);





    if($name == '') {

        $errmsg_arr[] = 'Name missing';

        $errflag = true;

    }

    if($toupld == '') {

        $errmsg_arr[] = 'Path missing';

        $errflag = true;

    }

    if($source == '') {

        $errmsg_arr[] = 'Type missing';

        $errflag = true;

    }



    if($Desc == '') {

        $errmsg_arr[] = 'Description missing';

        $errflag = true;

    }

    if($By == '') {

        $errmsg_arr[] = 'By missing';

        $errflag = true;

    }

    if($For == '') {

        $errmsg_arr[] = 'For missing';

        $errflag = true;

    }





    function findexts ($fname) { 

        $fname = strtolower($fname) ; 

        $extensions = split("[/\\.]", $fname) ; 

        $n = count($extensions)-1; 

        $extensions = $extensions[$n]; 

    }



    $target_path="../notice/";





    if($errflag) {

        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;

        session_write_close();

        header("location: ../err.php");

        exit();

    }

if($_FILES['file']['error']==0){



    if(($_FILES['file']['size']<1000000)&&($_FILES['file']['type']=="application/msword")||($_FILES['file']['type']=="application/pdf")){



        $ext=findtext($_FILES['file']['name']);

        $random=rand(0,5);

        $random2=$random.".";

        $target=$target.$random2.$ext;

        if(move_uploaded_file($_FILES['file']['tmp_name'],$target)){



            $qry="INSERT INTO PALMSIS.Notice(NoticeID,Name,Date,Timestamp,Type,Descr,By,For) VALUES('$random','$name',NOW(),NOW(),'$source','$Desc','$By','$For')";

            $result=@mysql_query($qry);

            if($result){

                header("location:./noticeupld.php");

                exit();

                }

                else{



                    die("query Failed");



                    }



            }

            else{



                header("location:./success.php");

                }

        }

        else{



            header("location:./noproper.php");



            }



    }

    else{



        header("location:./error.php");



        }?>


推荐答案

在这必须在1mb以下,这是很容易.pdf的图形超越...我将开始一些基本的故障排除技巧。

All I can think of off hand is that the files on this must be under 1mb which is easy for .pdf's with graphics to surpass... I'd start with some basic troubleshooting techniques.

例如:

if($_FILES['file']['error']==0){
    $flag = $flag+1;
    echo 'No errors reported on file upload<br>';
    }
if($_FILES['file']['size']<1000000){
    $flag = $flag+1;
    echo 'Size is confirmed under 1mb<br>';
    }
if(($_FILES['file']['type']=="application/msword")||($_FILES['file']['type']=="application/pdf")){
    $flag = $flag+1;
    echo 'Correct Filetype confirmed<br>';
    }

这样你就知道哪些通过了检查。

This way you know which ones are passing the checks.

祝你好运!

这篇关于文件上传器脚本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆