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

本文是小编为大家收集整理的关于文件上传器脚本不能正常工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用PHP和MySQL创建文件上传脚本.但是,此SRIPT向我展示了Err.php页面(当有任何输入验证错误时,该页面应该执行).我尝试对其进行调整,但没有机会.以下是我的php脚本

    <?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");



        }?>

推荐答案

我将从上传文件开始,看看是否有效...然后添加其他部件,看看麻烦从哪里开始.该代码假定您的表单具有一个名为" file_name"的文本字段,您指定您将文件移至服务器时要调用的内容以及您的文件字段.不要忘记您的表格必须是多部分.这是人们经常忘记的事情,是造成很多麻烦的原因.

$fileName = $_REQUEST[ 'file_name' ];
$folder = "./";


if ( move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ], $folder . $fileName ) ) {
        echo "SUCCESS:" . $folder.$fileName . " has been saved";

} 
else{
     echo "FAIL:";

}

其他推荐答案

我只能想到的是,此上的文件必须在1MB下,对于.pdf的图形很容易超越...我将从一些基本的故障排除技术开始.

例如:

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>';
    }

这样,您知道哪些正在通过检查.

祝你好运伴侣!

其他推荐答案

您可能不需要,但是最好将$errflag初始化为false和$errmsg_arr到空数组.

本文地址:https://www.itbaoku.cn/post/597367.html

问题描述

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");



        }?>

推荐答案

I would start with just uploading a file and see if that works... then add in your other pieces and see where the trouble begins. This code assumes you have a form that has a text field named "file_name" where you designate what you'd like the file to be called when it's moved to the server, as well as a file field that is your file. Don't forget that your form has to be multipart. That's something people frequently forget and is the cause of much trouble.

$fileName = $_REQUEST[ 'file_name' ];
$folder = "./";


if ( move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ], $folder . $fileName ) ) {
        echo "SUCCESS:" . $folder.$fileName . " has been saved";

} 
else{
     echo "FAIL:";

}

其他推荐答案

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.

for instance:

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.

Good luck mate!

其他推荐答案

You probably don't have to, but it would be good practice to initialize $errflag to false and $errmsg_arr to an empty array.