473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

PHP File Uploader

Jacotheron
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 3807
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
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 Expert 4TB
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
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
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
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
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
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...
13
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...
1
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...
0
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...
5
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...
1
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...
9
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...
1
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.