Php变量在UPLOAD脚本中不起作用

时间:2015-06-03 01:50:38

标签: php

我有以下脚本:

<?php

$userid = $_GET['user'];

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }


    if(move_uploaded_file($_FILES['upl']['tmp_name'], '../../'.$userid.'/assets/'.$_FILES['upl']['name'])){ 

        echo '{"status":"success"}';
        echo ' '.$userid;
        exit;
    }
}

echo '{"status":"error"}';
echo ' '.$userid;
exit;

?>

问题出在这一行:

    if(move_uploaded_file($_FILES['upl']['tmp_name'], '../../'.$userid.'/assets/'.$_FILES['upl']['name'])){

当我使用此路径时 - 一切正常并且上传成功:

'../../assets/'.$_FILES['upl']['name']

'../assets/'.$_FILES['upl']['name']

'assets/'.$_FILES['upl']['name']

所有这三个工作正常 - 但是当我从GET(从URL路径)添加VARIABLE时,它不起作用?我知道VARIABLE“userid”正在被识别,因为我在路径之前和之后回显,它显示在屏幕上。此外,我DID设置了对VARIABLE定义的所有文件夹的写权限。

有什么想法吗?

已添加&gt;&gt;&gt;&gt;&gt;&gt;这是第一个文件......

<?

$userid = $_GET['user'];

?>

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8"/>
        <title>Mini Ajax File Upload Form</title>

        <!-- Google web fonts -->
        <link href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700" rel='stylesheet' />

        <!-- The main CSS file -->
        <link href="css/style.css" rel="stylesheet" />
    </head>

    <body>
    <p style="text-align:center"><? echo $userid; ?></p>

        <form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
            <div id="drop">
                Drop Here

                <a>Browse</a>
                <input type="file" name="upl" multiple />
            </div>

            <ul>
                <!-- The file uploads will be shown here -->
            </ul>

        </form>


        <!-- JavaScript Includes -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="js/jquery.knob.js"></script>

        <!-- jQuery File Upload Dependencies -->
        <script src="js/jquery.ui.widget.js"></script>
        <script src="js/jquery.iframe-transport.js"></script>
        <script src="js/jquery.fileupload.js"></script>

        <!-- Our main JS file -->
        <script src="js/script.js"></script>


    </body>
</html>

这是处理文件......

<?php

$userid = $_GET['user'];

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    // if(move_uploaded_file($_FILES['upl']['tmp_name'], '../../'.$userid.'/assets/'.$_FILES['upl']['name'])){  
    if(move_uploaded_file($_FILES['upl']['tmp_name'], '../../'.$userid.'/assets/'.$_FILES['upl']['name'])){ 

        echo '{"status":"success"}';
        echo ' '.$userid;
        exit;
    }
}

echo '{"status":"error"}';
echo ' '.$userid;
exit;

?>

1 个答案:

答案 0 :(得分:0)

除非您手动为每个用户在../../创建一个子目录,否则您需要在尝试将文件移动到该用户之前为该用户创建目录。

if(!file_exist("../../$userid/")
    mkdir("../../$userid/"); 

...然后您可以将文件移动到它。