我正在尝试制作一个用于mp3上传的PHP脚本,但是我想使用用户会话$ _SESSION [username]创建一个不存在的上传文件夹。 在文件夹mp3 /中,当我在命令行运行时我没有出现任何错误,但是当我尝试上载mp3时,它每次都会失败,并且不会创建该文件夹。
<?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>\n";
}
else
{
print "<p>Upload failed, sorry</p>\n";
}
print <<<END
<p>
To continue, <a href="index.php">click here.</a>
</p>
</body>
</html>
END;
?>
答案 0 :(得分:0)
您使用的目录路径错误。
使用此:
// lets get our posts //
$song = $_FILES['song_name'];
// folder that will hold songs
$songdir = '/mp3/' . $_SESSION['username'] . '/';
// song-file pathname
$songpath = $songdir . $song['name'];
if (!file_exists($songdir)) {
mkdir($songdir, 0777, true);
}
答案 1 :(得分:0)
检查phpinfo(); 什么是tmp目录?还要检查max_file_size并检查所有路由的用户权限
答案 2 :(得分:0)
所以我能够使用代码,并在朋友的一点帮助下使其工作 谢谢的Oldcoder。简而言之,我必须给出绝对路径,如果没有,请检查mp3文件夹是否存在,然后检查用户是否有一个文件夹/ mp3 / user 似乎工作得很好:)
<?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>\n";
}
else
{
print "<p>Upload failed, sorry</p>\n";
print "<p>($mp3dir) ($songdir) ($songpath)</p>";
}
print <<<END
<p>
To continue, <a href="index.php">click here.</a>
</p>
</body>
</html>
END;
?>