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;
?>
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;
?>
You're using the wrong path for the directory.
Use this:
// 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);
}
Check phpinfo(); What is tmp directory? Also check max_file_size and check your user right for all route