473,725 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP File Uploader

Jacotheron
44 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // filename: upload.form.php
  3. // make a note of the current working directory relative to root.
  4. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  5. // make a note of the location of the upload handler
  6. $uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.processor.php';
  7. // set a max file size for the html upload form
  8. $max_file_size = 28311552; // size in bytes (27 MB max)
  9. ?>
  10. <html lang="en">
  11. <head>
  12. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  13. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  14. <title>Upload form</title>
  15. </head>
  16. <body>
  17. <form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
  18. <h1>
  19. Upload form
  20. </h1>
  21. <p>
  22. <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
  23. </p>
  24. <p>
  25. <label for="file">File to upload:</label>
  26. <input id="file" type="file" name="file">
  27. </p>
  28. <p>Artist:<input type="text" name="artist" size="24"></p>
  29. <p>Title:<input type="text" name="title" size="24"></p>
  30. <p>Album:<input type="text" name="album" size="24"></p>
  31. <p>Lenth:<input type="text" name="lenth" size="24"></p>
  32. <p>Genre:<input type="text" name="genre" size="24"></p>
  33. <p>
  34. <label for="submit">Press to...</label>
  35. <input id="submit" type="submit" name="submit" value="Upload me!">
  36. </p>
  37. </form>
  38. </body>
  39. </html>
  40.  
  41. <?php 
  42. // filename: upload.processor.php
  43. // make a note of the current working directory, relative to root.
  44. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  45. // make a note of the directory that will recieve the uploaded files
  46. $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/$artist/';
  47. // make a note of the location of the upload form in case we need it
  48. $uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.form.php';
  49. // make a note of the location of the success page
  50. $uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'upload.success.php';
  51. // name of the fieldname used for the file in the HTML form
  52. $fieldname = 'file';
  53. // possible PHP upload errors
  54. $errors = array(1 => 'php.ini max file size exceeded', 
  55.                 2 => 'html form max file size exceeded', 
  56.                 3 => 'file upload was only partial', 
  57.                 4 => 'no file was attached');
  58. // check the upload form was actually submitted else print form
  59. isset($_POST['submit'])
  60. or error('the upload form is neaded', $uploadForm);
  61. // check for standard uploading errors
  62. ($_FILES[$fieldname]['error'] == 0)
  63. or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
  64. // check that the file we are working on really was an HTTP upload
  65. @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
  66. or error('not an HTTP upload', $uploadForm);
  67. // make a unique filename for the uploaded file and check it is 
  68. // not taken... if it is keep trying until we find a vacant one
  69. $now = time();
  70. while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))
  71. {
  72. $now++;
  73. }
  74. // now let's move the file to its final and allocate it with the new filename
  75. @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
  76. or error('receiving directory insuffiecient permission', $uploadForm);
  77. // We are now going to redirect the client to the success page.
  78. header('Location: ' . $uploadSuccess);
  79. // make an error handler which will be used if the upload fails
  80. function error($error, $location, $seconds = 5)
  81. {
  82. header("Refresh: $seconds; URL=\"$location\"");
  83. echo 
  84. '<html lang="en">'."\n".
  85. ' <head>'."\n".
  86. ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
  87. ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
  88. ' <title>Upload error</title>'."\n\n".
  89. ' </head>'."\n\n".
  90. ' <body>'."\n\n".
  91. ' <div id="Upload">'."\n\n".
  92. ' <h1>Upload failure</h1>'."\n\n".
  93. ' <p>An error has occured: '."\n\n".
  94. ' <span class="red">' . $error . '...</span>'."\n\n".
  95. ' The upload form is reloading</p>'."\n\n".
  96. ' </div>'."\n\n".
  97. '</html>';
  98. exit;
  99. } // end error handler
  100. ?>
  101.  
  102. <?php
  103. // filename: upload.success.php
  104. ?>
  105. <html lang="en">
  106. <head>
  107. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  108. <link rel="stylesheet" type="text/css" href="stylesheet.css">
  109. <title>Successful upload</title>
  110. </head>
  111. <body>
  112. <div id="Upload">
  113. <h1>File upload</h1>
  114. <p>Congratulations! Your file upload was successful</p>
  115. </div>
  116. </body>
  117. </html>
  118.  
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.
Jun 23 '08 #1
6 3835
nashruddin
25 New Member
Here's the code snippet to rename the uploaded file to ARTIST - TITLE.mp3 and move it to destination folder.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* obtain artist name and title from previous form */
  3. $artist   = $_POST['artist'];
  4. $title    = $_POST['title'];
  5.  
  6. /* new name for the uploaded mp3 */
  7. $filename = "$artist - $title.mp3";
  8.  
  9. /* change to the directory of your mp3s */
  10. $destpath = "C:/www/uploads/mp3";
  11.  
  12. /* move uploaded file to destination path */
  13. if (is_uploaded_file($_FILES['file']['tmp_name'])) {
  14.     move_uploaded_file($_FILES['file']['tmp_name'], "$destpath/$filename");
  15. }
  16. ?>
  17.  
Jun 24 '08 #2
Jacotheron
44 New Member
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
Jun 25 '08 #3
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()

:)
Jun 25 '08 #4
Jacotheron
44 New Member
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
Jun 25 '08 #5
nashruddin
25 New Member
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $dir = '/some/path/to/mp3';
  3.  
  4. if (is_dir($dir)) {
  5.   echo "directory exists";
  6. } else {
  7.   echo "directory doesn't exist.";
  8.   /* and create the directory */
  9.   mkdir($dir, 0700);
  10. }
  11. ?>
  12.  
Jun 29 '08 #6
Jacotheron
44 New Member
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $dir = '/some/path/to/mp3';
  3.  
  4. if (is_dir($dir)) {
  5.   echo "directory exists";
  6. } else {
  7.   echo "directory doesn't exist.";
  8.   /* and create the directory */
  9.   mkdir($dir, 0700);
  10. }
  11. ?>
  12.  
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
Jun 30 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2196
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
2
1428
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'??
13
4315
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...
1
2722
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:
0
2315
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...
5
13887
chunk1978
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" />
1
1883
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 />
9
2811
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...
1
1552
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.
7
7153
Curtis Rutland
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...
0
8888
marktang
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...
0
8752
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,...
0
9401
Oralloy
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...
0
9257
jinu1996
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...
0
8097
agi2029
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...
0
6011
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();...
0
4519
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.