Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP File Uploader

Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#1: Jun 23 '08
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.

Newbie
 
Join Date: Jun 2008
Posts: 25
#2: Jun 24 '08

re: PHP File Uploader


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.  
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#3: Jun 25 '08

re: PHP File Uploader


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's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#4: Jun 25 '08

re: PHP File Uploader


Quote:

Originally Posted by Jacotheron

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()

:)
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#5: Jun 25 '08

re: PHP File Uploader


Quote:

Originally Posted by markusn00b

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
Newbie
 
Join Date: Jun 2008
Posts: 25
#6: Jun 29 '08

re: PHP File Uploader


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.  
Jacotheron's Avatar
Member
 
Join Date: Jan 2008
Location: Gauteng, South Africa
Posts: 42
#7: Jun 30 '08

re: PHP File Uploader


Quote:

Originally Posted by nashruddin

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
Reply