bryceray1121 Posted January 16, 2009 Share Posted January 16, 2009 I have a script to upload mp3 files to my server through a online form however i'm having some difficulty. I understand what most of the script does but i'm having a few issues. I've nearly got the script working. However, it is still not uploading the file. The script did execute without an error message though. When i looked in my mysql database where i want it to just put the modified name of the file it put this:/homepages/31/d22100 instead of 12345-filename.mp3 (for example). And it was not uploaded to the directory, any suggestions? EDIT: After looking at the specifications for move_uploaded_file() i see that the variable $uploadFilename is actually the destination folder not the filename. But i'm still not sure how the name of the file is created and the file is renamed. And then i'm not sure which variable should contain the filename so i can save it to my database. I see that $_FILES[$fieldname]['tmp_name'] is supposed to be the filename but when i save that to my mysql database i get this: /tmp/phpkOjo5J or somethign similar. And the file is still not uploading, could have something to do with the filenaming problems though. // filename: upload.processor.php // first let's set some variables // 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 file $uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . 'media/mp3player/music/'; // make a note of the location of the upload form in case we need it $uploadForm = 'editmp3.php'; // fieldname used within the file <input> of the HTML form $fieldname = 'file'; // Now let's deal with the upload // 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'); if($_POST['submitadd']) { // check for PHP's built-in uploading errors ($_FILES[$fieldname]['error'] == 0) or error($errors[$_FILES[$fieldname]['error']], $uploadForm); // check that the file we are working on really was the subject of 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 already // taken... if it is already taken keep trying until we find a vacant one // sample filename: 1140732936-filename.jpg $now = time(); while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])) { $now++; } // now let's move the file to its final location and allocate the new filename to it @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) or error('receiving directory insuffiecient permission', $uploadForm); $sql = "INSERT INTO `music` ( `id` , `title` , `composer` , `venue`, `file`, `active`) VALUES ('','" . $_POST['title'] . "', '" . $_POST['composer'] . "', '" . $_POST['venue'] . "', '" . $uploadFilename . "', '" . $_POST['active'] . "')"; mysql_query($sql); header("Location: editmp3.php?msg=1"); // The following function is an error handler which is used // to output an HTML error page if the file upload fails function error($error, $location, $seconds = 5) { header("Refresh: $seconds; URL=\"$location\""); echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n". '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n". '<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 }else { require("backendheader.php"); ?> <div id="leftnav"> <?php require("leftnav.php"); ?> </div> <!-- End of leftnav id --> <div id="main"> <?php if($validentry!=0){ $fillsql = "SELECT * FROM music WHERE id = " . $validentry . ";"; $fillres = mysql_query($fillsql); $fillrow = mysql_fetch_assoc($fillres); echo "<h1>Edit Song</h1>"; echo "<p>click<a href='editmp3.php'> here </a> to add a new song</p>"; } else{ echo "<h1>Add New Song</h1>"; } //********************************** //Check if a confirmation message needs to be printed //********************************** if($_GET['msg'] == 1){ echo "<h5 class='confirm'>Song Succesfully Added</h5>"; } if($_GET['msg'] == 2){ echo "<h5 class='confirm'>Song Succesfully Edited</h5>"; } if($_GET['msg'] == 3){ echo "<h5 class='confirm'>Song Succesfully Deleted</h5>"; } ?> <form action="<?php echo $SCRIPT_NAME ?>" enctype="multipart/form-data" method="post"> <table> <input type="hidden" name="id" value="<?php echo $validentry;?>" /> <tr> <td>Title: </td> <td> <input type="text" name="title" size="50" value="<?= trim($fillrow['title']); ?>"> </td> </tr> <tr> <td>Composer: </td> <td> <input type="text" name="composer" size="50" value="<?= trim($fillrow['composer']); ?>"> </td> </tr> <tr> <td>Venue:</td> <td> <input type="text" name="venue" size="50" value="<?= trim($fillrow['venue']); ?>"> </td> </tr> <tr> <td>File:</td> <td> <input type="file" name="file"> <input type="hidden" name="MAX_FILE_SIZE" value="25000" /> </td> </tr> <tr> <td>Active:</td> <td> <select name="active"> <option value="yes">yes</option> <option value="no">no</option> </select> </td> </tr> <tr> <td></td> <td> <?php if($validentry!=0){ echo "<input type='submit' name='submitedit' value='Edit Song' />"; } else{ echo "<input type='submit' name='submitadd' value='Add Song' />"; } ?> </td> </tr> </table> </form> Thanks for your help i've been stuck on this upload thing forever and i'm getting very frustrated with it. Link to comment https://forums.phpfreaks.com/topic/141112-uploading-files-to-my-server/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.