dongrong7267 2018-09-09 02:45
浏览 52
已采纳

使用php检查文件夹是否存在,如果没有在文件上传脚本上

I'm trying to make a PHP script for mp3 uploads, But I want to make a folder on upload if one does not exists, using the user's session $_SESSION[username] in folder mp3/, when i run at the command line i get no error's but when i try to up load a mp3 it fails everytime ,and dose not make the folder .

<?php 


session_start();

if (isset      ($_SESSION ['band_id'  ]))
{
    $band_id  = $_SESSION ['band_id'  ];
    $bandname = $_SESSION ['bandname' ];
    $username = $_SESSION ['username' ];
}

// set database connection

require("connect.php");



// lets get our posts //
$song     = $_FILES['song_name'];
// folder that will hold songs
$songpath = '/mp3/' . $_SESSION['username'];

// song-file pathname
$songpath .= $song['name'];

if (!file_exists($songpath)) {
    mkdir($songpath."/".$_SESSION['username'], "/" ,0777,true);
}

var_dump($songpath);

// move the file from the tmp folder to the song folder
if (move_uploaded_file ($song['tmp_name'], $songpath))
{
    print "<p>Upload succeeded thank you</p>
";
}
else
{
    print "<p>Upload failed, sorry</p>
";
}

print <<<END
<p>
To continue, <a href="index.php">click here.</a>
</p>
</body>
</html>
END;

?>
  • 写回答

3条回答 默认 最新

  • dqkv0603 2018-09-11 04:00
    关注

    so I was able to play around with the code, got it to work with a little help from a friend Thank's Oldcoder. In a nutshell, I had to give the absolute path, check to see if the mp3 folder is there if not make one, then check if the user had a folder /mp3/user seems to work pretty well :)

    <?php
    
    
    
    
    session_start();
    
    if (isset      ($_SESSION ['band_id'  ]))
    {
        $band_id  = $_SESSION ['band_id'  ];
        $bandname = $_SESSION ['bandname' ];
        $username = $_SESSION ['username' ];
    }
    
    // set database connection
    
    require("connect.php");
    
    
    
    // lets get our posts //
    $song     = $_FILES["song_file"]["name"];
    // Main MP3-files directory
    $mp3dir   = '/var/www/ympradio.com/public_html/mp3';
    
    // Create it, if necessary
    if (!file_exists ($mp3)) {
        mkdir ($mp3dir, 0777, true);
    }
    
    // Folder that will hold user's files
    $songdir = $mp3dir . '/' . $_SESSION ['username'];
    
    // Create it, if necessary
    if (!file_exists ($songdir)) {
        mkdir ($songdir, 0777, true);
    }
    
    // Song-file pathname
    $songpath = $songdir . '/' . $song;
    
    var_dump($songpath);
    
    $songtemp = $_FILES["song_file"]["tmp_name"];
    
    // move the file from the tmp folder to the song folder
    if (move_uploaded_file ($songtemp, $songpath))
    {
        print "<p>Upload succeeded thank you</p>
    ";
    }
    else
    {
        print "<p>Upload failed, sorry</p>
    ";
        print "<p>($mp3dir) ($songdir) ($songpath)</p>";
    }
    
    print <<<END
    <p>
    To continue, <a href="index.php">click here.</a>
    </p>
    </body>
    </html>
    END;
    
    ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程