I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the network should be able to connect to the database and run queries on the database or upload new music that does not yet exist on the database. The uploaded file's name should be in the following format: ARTIST - TITLE.mp3. I have the code to upload images, but have no idea what to change to make it music compatible and correctly rename the file and move it to the correct folder. Here is the code I have: -
<?php
-
// filename: upload.form.php
-
// make a note of the current working directory relative to root.
-
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
-
// make a note of the location of the upload handler
-
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.processor.php';
-
// set a max file size for the html upload form
-
$max_file_size = 28311552; // size in bytes (27 MB max)
-
?>
-
<html lang="en">
-
<head>
-
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
-
<link rel="stylesheet" type="text/css" href="stylesheet.css">
-
<title>Upload form</title>
-
</head>
-
<body>
-
<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
-
<h1>
-
Upload form
-
</h1>
-
<p>
-
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
-
</p>
-
<p>
-
<label for="file">File to upload:</label>
-
<input id="file" type="file" name="file">
-
</p>
-
<p>Artist:<input type="text" name="artist" size="24"></p>
-
<p>Title:<input type="text" name="title" size="24"></p>
-
<p>Album:<input type="text" name="album" size="24"></p>
-
<p>Lenth:<input type="text" name="lenth" size="24"></p>
-
<p>Genre:<input type="text" name="genre" size="24"></p>
-
<p>
-
<label for="submit">Press to...</label>
-
<input id="submit" type="submit" name="submit" value="Upload me!">
-
</p>
-
</form>
-
</body>
-
</html>
-
-
<?php
-
// filename: upload.processor.php
-
// make a note of the current working directory, relative to root.
-
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
-
// make a note of the directory that will recieve the uploaded files
-
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/$artist/';
-
// make a note of the location of the upload form in case we need it
-
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';
-
// make a note of the location of the success page
-
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
-
// name of the fieldname used for the file in the HTML form
-
$fieldname = 'file';
-
// possible PHP upload errors
-
$errors = array(1 => 'php.ini max file size exceeded',
-
2 => 'html form max file size exceeded',
-
3 => 'file upload was only partial',
-
4 => 'no file was attached');
-
// check the upload form was actually submitted else print form
-
isset($_POST['submit'])
-
or error('the upload form is neaded', $uploadForm);
-
// check for standard uploading errors
-
($_FILES[$fieldname]['error'] == 0)
-
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
-
// check that the file we are working on really was an HTTP upload
-
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
-
or error('not an HTTP upload', $uploadForm);
-
// make a unique filename for the uploaded file and check it is
-
// not taken... if it is keep trying until we find a vacant one
-
$now = time();
-
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
-
{
-
$now++;
-
}
-
// now let's move the file to its final and allocate it with the new filename
-
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
-
or error('receiving directory insuffiecient permission', $uploadForm);
-
// We are now going to redirect the client to the success page.
-
header('Location: ' . $uploadSuccess);
-
// make an error handler which will be used if the upload fails
-
function error($error, $location, $seconds = 5)
-
{
-
header("Refresh: $seconds; URL=\"$location\"");
-
echo
-
'<html lang="en">'."\n".
-
' <head>'."\n".
-
' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
-
' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
-
' <title>Upload error</title>'."\n\n".
-
' </head>'."\n\n".
-
' <body>'."\n\n".
-
' <div id="Upload">'."\n\n".
-
' <h1>Upload failure</h1>'."\n\n".
-
' <p>An error has occured: '."\n\n".
-
' <span class="red">' . $error . '...</span>'."\n\n".
-
' The upload form is reloading</p>'."\n\n".
-
' </div>'."\n\n".
-
'</html>';
-
exit;
-
} // end error handler
-
?>
-
-
<?php
-
// filename: upload.success.php
-
?>
-
<html lang="en">
-
<head>
-
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
-
<link rel="stylesheet" type="text/css" href="stylesheet.css">
-
<title>Successful upload</title>
-
</head>
-
<body>
-
<div id="Upload">
-
<h1>File upload</h1>
-
<p>Congratulations! Your file upload was successful</p>
-
</div>
-
</body>
-
</html>
-
Please help me to get this script ready for my database. I know how to insert a new entry in the database but that would be done later after the main part do what it should do.
6 3835
Here's the code snippet to rename the uploaded file to ARTIST - TITLE.mp3 and move it to destination folder. -
<?php
-
/* obtain artist name and title from previous form */
-
$artist = $_POST['artist'];
-
$title = $_POST['title'];
-
-
/* new name for the uploaded mp3 */
-
$filename = "$artist - $title.mp3";
-
-
/* change to the directory of your mp3s */
-
$destpath = "C:/www/uploads/mp3";
-
-
/* move uploaded file to destination path */
-
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
-
move_uploaded_file($_FILES['file']['tmp_name'], "$destpath/$filename");
-
}
-
?>
-
How do I specify that the PHP should create the directory if the directory does not exist. I want all music to also be sorted ARTIST/ALBUM/ARTIST - TITLE.mp3. Every Thing works well when I create the path myself but the script should do it.
Thank you for the help
Markus 6,050
Recognized Expert Expert
How do I specify that the PHP should create the directory if the directory does not exist. I want all music to also be sorted ARTIST/ALBUM/ARTIST - TITLE.mp3. Every Thing works well when I create the path myself but the script should do it.
Thank you for the help
Have a look at is_dir()
:)
Have a look at is_dir()
:)
Now I could test if the directory exist, but how do I command PHP to create the directory if it does not exist? That is my biggest problem.
Thank you for the help
-
<?
-
$dir = '/some/path/to/mp3';
-
-
if (is_dir($dir)) {
-
echo "directory exists";
-
} else {
-
echo "directory doesn't exist.";
-
/* and create the directory */
-
mkdir($dir, 0700);
-
}
-
?>
-
-
<?
-
$dir = '/some/path/to/mp3';
-
-
if (is_dir($dir)) {
-
echo "directory exists";
-
} else {
-
echo "directory doesn't exist.";
-
/* and create the directory */
-
mkdir($dir, 0700);
-
}
-
?>
-
When I enter this information, and run it with WAMP, I get the following error message: "Warning: mkdir() [function.mkdir]: No such file or directory in the directory". I have changed the $dir to my variable and it just do not want to create the directory. It seems that it is looking for a function that describes exactly what it should do.
Thank you for the help
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: M P |
last post by:
Can you help me find an asp code that will upload a file from my PC to web
server?
Mark
|
by: Andy |
last post by:
Hi
I have recently developed a few sites with an uploader
facility for images and all has worked perfectly, until
now???
I re-installed .NET due to an unrelated problem and since
then if I use a WebServer Button control to submit the
form I have nothing within the Html uploader component
it's 'null'??
|
by: Sky Sigal |
last post by:
I have created an IHttpHandler that waits for uploads as attachments for a
webmail interface, and saves it to a directory that is defined in
config.xml.
My question is the following:
assuming that this is suppossed to end up as a component for others to use,
and therefore I do NOT have access to their global.cs::Session_End()
how do I cleanup files that were uploaded -- but obviously left stranded
when the users aborted/gave up writting...
|
by: pbd22 |
last post by:
hi.
i have been posting this here and elsewhere a lot and can't seem to get
resolution on this problem.
i have been trying to upload files using a hidden iframe to a
asp.net/vb.net form. the problem is that
the server code can't read the httpfilecollection. the count is always
zero.
my upload form's form tag looks like this:
|
by: wasif |
last post by:
I am trying to upload file using ajax and php but having some problems. it always says that there was a problem and file is not uploaded. here is the code
form and ajax code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled...
| |
by: chunk1978 |
last post by:
hi there...
i have the following codes (HTML and PHP) on my Apache Localhost:
HTML: titled "Form.html"
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
|
by: recordlovelife |
last post by:
Hi all.
I need to make a file uploader, so a client can upload pictures to a directory on their shared server, so that they can later include the photos in news updates.
I simply want to take this form:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
|
by: saldandm |
last post by:
I think this is probably some minor oversight on my end but I'm just missing it.
I have a multipart/form-data form in a ASP page. Inside the form I have traditional text fields and a upload field for uploading images to a server.
The intent is to store the image name (not the image itself) along with corresponding data in a SQL Server database.
The problem I'm currently having is that the upload is working correctly but it is not...
|
by: groupie |
last post by:
Hi,
I'm using the excellent Multiple file uploader (
http://the-stickman.com/web-development/javascript/multiple-file-uploader-mootools-version/
)
After selecting the files (as per the Defaults example on
http://the-stickman.com/files/mootools/multiupload/ ), I use a Submit
button to send the data to a second page: how can I retrieve the file
names entered on the first page, as I don't understand the code that
well. Thanks.
|
by: Curtis Rutland |
last post by:
Building A Silverlight (2.0) Multi-File Uploader
All source code is C#. VB.NET source is coming soon.
Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and Silverlight 2.0. To get these tools please visit this page
Get Started : The Official Microsoft Silverlight Site
and follow Step 1.
Occasionally you find the need to have users upload multiple files at once. You could use multiple FileUpload...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |