dongyuanliao6204 2017-03-13 09:38
浏览 99

未定义的索引:第77行的C:\ xampp \ htdocs \ login.php中的扩展名

I am trying to make a file upload for my website, and it has been working for the past few weeks now until today when i tried to change the upload directory. Now it says Undefined index: extension so i tried changing it back but it still says it. My code is here:

login.php

<!DOCTYPE html>
<head>
<style>

   
</style>
<body>

 


<?php

session_start();
$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';
if(!empty($sessData['status']['msg'])){
    $statusMsg = $sessData['status']['msg'];
    $statusMsgType = $sessData['status']['type'];
    unset($_SESSION['sessData']['status']);
}
?>
<div class="container">
    <?php
        if(!empty($sessData['userLoggedIn']) && !empty($sessData['userID'])){
            include 'user.php';
            $user = new User();
            $conditions['where'] = array(
                'id' => $sessData['userID'],
            );
            $conditions['return_type'] = 'single';
            $userData = $user->getRows($conditions);
    ?>

    <h2>Welcome <?php echo $userData['first_name']; ?>!</h2>
    <a href="userAccount.php?logoutSubmit=1" class="logout">Logout</a>
    <div class="regisFrm">
        <p><b>Username: </b><?php echo $userData['username']; ?></p>
        <p><b>Name: </b><?php echo $userData['first_name'].' '.$userData['last_name']; ?></p>
        <p><b>Email: </b><?php echo $userData['email']; ?></p>
        <p><b>Phone: </b><?php echo $userData['phone']; ?></p>
    </div>
    <?php
    $user = $userData['username'];
    $url = "/Users/makefile.php?uname=$user";
    
    
echo '<a href="'. $url .'" class="btn btn-default">Your Account</a>';
// echo '<a href="'. $url . '">Your Account</a>';
?>
    <form action="upload.php?user=$user" method="post" enctype="multipart/form-data"> 
 <input type="file" name="myFile">
 <br>
 <input type="submit" value="Upload">
</form>
<?php 
$dir_path = "uploads/";
$extensions_array = array('jpg','png','jpeg','PNG','mp3','MP3','mp4','MP4');

if(is_dir($dir_path))
{
    $files = scandir($dir_path);
    
    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] !='.' && $files[$i] !='..')
        {
            // get file name
            // echo "File Name -> $files[$i]<br>";
            
            // get file extension
            $file = pathinfo($files[$i]);
            $extension = $file['extension'];
            // echo "File Extension-> $extension<br>";
            $filephp = $files[$i] . '.php';
            $filetxt = $files[$i] . '.txt';
            $fileimg = $files[$i] . '.jpg';
            $filetxtu = $files[$i] . 'uploaded' . '.txt';

           // check file extension
            if(in_array($extension, $extensions_array))
            {
            // show image
            echo "<center><a href='$filephp?txt=$filetxt&img=$files[$i]&php=$filephp&user=$user&txtu=$filetxtu' target='_blank'>
            <img src='$dir_path$files[$i]' style='width:300px;height:300px;align:left;'><br></a>
            </center>";
            
            }
        }
    }
}

?>
     
    <?php }else{ ?>
    <h2>Login to Your Account</h2>
    <?php echo !empty($statusMsg)?'<p class="'.$statusMsgType.'">'.$statusMsg.'</p>':''; ?>
    <div class="regisFrm">
        <form action="userAccount.php?uimg=$" method="post">
            <input type="email" name="email" placeholder="EMAIL" required="">
            <input type="password" name="password" placeholder="PASSWORD" required="">
            <div class="send-button">
                <input type="submit" name="loginSubmit" value="LOGIN">
            </div>
        </form>
        <p>Don't have an account? <a href="registration.php?user=$user">Register</a></p>
        <p>Go back home <a href="index.html">Here</a></p>
    </div>
    <?php } ?>
</div>

upload.php

<?php
$uname = $_GET['user'];
define("UPLOAD_DIR", "/uploads");

if (!empty($_FILES["myFile"])) {
    $myFile = $_FILES["myFile"];

    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }

    // ensure a safe filename
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    // don't overwrite an existing file
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }
    $nametxt = $name . ".txt";
    $namephp = $name . ".php";
    $nametxtu = $name . "uploaded" . ".php";
    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"],
        UPLOAD_DIR . $name);
$content = " ";
$fp = fopen($nametxt, "wb");
if( $fp == false ){
    //do debugging or logging here
}else{
    fwrite($fp,$content);
    fclose($fp);
}
 $text = file_get_contents('comments.php');
 $paste = file_put_contents($namephp, $text);
if($paste)
{
 echo "File copied correctly
";
} else {
 echo "There was a problem copying the file
";
}
$file = fopen($nametxtu,"w");
echo fwrite($file, $uname);
fclose($file);
header("Location: login.php");
die();
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }

    // set proper permissions on the new file
    chmod(UPLOAD_DIR . $name, 0644);
    header("Location: uploadyourown.php");
die();
}

</div>
  • 写回答

1条回答 默认 最新

  • dsa122870 2017-03-13 10:03
    关注

    Check that upload directory has only files you uploaded, may be directory contains empty file, and be sure all files you uploaded have extensions

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名