Jump to content

upload form and mysql


leequalls

Recommended Posts

ok the code below is uploading the files however it is not adding the info to the mysql database. Also when enabled the mime-type of audio/mpeg says that a mp3 file is not allowed.

 

$file = basename( $_FILES['uploaded']['name']);
$count = 0;
$rs2 = mysql_query("SELECT * FROM upload");
$count = mysql_num_rows($rs2);
do {
       $file = str_replace(' ', '_', $file, $count);
   } while ($count);
$genre = $_POST["genre"];
if ($w == "dj") { $target = "$djdir";   } else { $target = "$artdir"; }
$target = $target.$file ;

$ok=1; 

This is our limit file type condition
if (!($uploaded_type=="audio/mpeg")) {
echo "You may only upload MP3 files.<br>";
$ok=0;
} 

//This is our size condition
if ($uploaded_size > 10240000) // 10 MB
{
$error = $error . "Your file is too large.<br>";
$ok=0;
} 

if ($ok==0) { Echo "$error"; } 

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
for ($i = 0; $i < $count; $i++) {
$ufile = mysql_result($rs2, $i, 'file');
if ($ufile == $file) { mysql_query("UPDATE upload set (file, date, name, genre) values ('$file','$date','$username','$genre') where file='$file'"); $done = 'true'; 
echo "The file $file has been updated"; $insert = 0;} else { $insert = 1; }
}
if ($insert == 1) { mysql_query("INSERT INTO upload (file, date, name, genre) values ('$file','$date','$username','$genre')"); 
echo "The file $file has been uploaded";
}
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
} 
}

Link to comment
https://forums.phpfreaks.com/topic/169533-upload-form-and-mysql/
Share on other sites

I think the logics in this code has problem. please review the steps. I am not following your code, it is a bit unclear I have cleaned it a bit:

<?php   
$genre = $_POST["genre"];
if ($w == "dj") { $target = "$djdir";   } else { $target = "$artdir"; }

$file = basename( $_FILES['uploaded']['name']);
$count = 0;
// WHAT IS THIS BLOCK FOR 
$rs2 = mysql_query("SELECT * FROM upload");
$count = mysql_num_rows($rs2);
do {
	   $file = str_replace(' ', '_', $file, $count);
   } while ($count);
//-------------------------------------
$target = $target.$file ;
$ok=1;
echo "This is our limit file type condition";
if (!($uploaded_type=="audio/mpeg")) {
	echo "You may only upload MP3 files.<br>";
	$ok=0;
}

//This is our size condition
if ($uploaded_size > 10240000) // 10 MB
{
	$error = $error . "Your file is too large.<br>";
	$ok=0;
}

if ($ok==0) { 
	echo "$error"; 
} else {
	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
		for ($i = 0; $i < $count; $i++) {
			$ufile = mysql_result($rs2, $i, 'file'); // WHY DO YOU NEED THIS
			if ($ufile == $file) { mysql_query("UPDATE upload set (file, date, name, genre) values ('$file','$date','$username','$genre') where file='$file'"); $done = 'true';
				echo "The file $file has been updated"; $insert = 0;} else { $insert = 1; }
			}
			if ($insert == 1) { mysql_query("INSERT INTO upload (file, date, name, genre) values ('$file','$date','$username','$genre')");
				echo "The file $file has been uploaded";
			}
	} else {
		echo "Sorry, there was a problem uploading your file.";
	}
}
} // WHERE IS THIS ONE STARTED ?
?>

Sorry I will try to explain a little better:

 

if ($_POST['form'] == 'upload') {  //if form submitted continue

$file = basename( $_FILES['uploaded']['name']); //file being uploaded

$count = 0; //assigning $count to 0 

$rs2 = mysql_query("SELECT * FROM upload"); //database for uploaded file names

$count = mysql_num_rows($rs2); //how many files

//replace white space with underscore
do {
       $file = str_replace(' ', '_', $file, $count);
   } while ($count);

$genre = $_POST["genre"]; //get genre from form

//assign file directory
if ($w == "dj") { $target = "$djdir";   } else { $target = "$artdir"; }

$target = $target.$file; //full file path 

$ok=1; //assign ok to 1

//This is our limit file type condition *Does not let me upload MP3s?*
if (!($uploaded_type=="audio/mpeg")) {
echo "You may only upload MP3 files.<br>";
$ok=0;
} 

//This is our size condition
if ($uploaded_size > 10240000) // 10 MB
{
$error = $error . "Your file is too large.<br>";
$ok=0;
} 

if ($ok==0) { Echo "$error"; } 

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
//the for loop checks the database to see if the file already exist 
for ($i = 0; $i < $count; $i++) {
$ufile = mysql_result($rs2, $i, 'file');
if ($ufile == $file) { mysql_query("UPDATE upload set (file, date, name, genre) values ('$file','$date','$username','$genre') where file='$file'"); $done = 'true'; 
echo "The file $file has been updated"; $insert = 0;} else { $insert = 1; }
}

//if the file does not exist already insert into database
if ($insert == 1) { mysql_query("INSERT INTO upload (file, date, name, genre) values ('$file','$date','$username','$genre')"); 
echo "The file $file has been uploaded";
}
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
} 

//form for upload

echo("<p>");
if ($w == 'dj') { echo("<form enctype=multipart/form-data action=?p=upload&w=dj method=POST><table><tr><td><input name=\"uploaded\" type=\"file\" /></td><td><input name=genre type=hidden value=id>"); } 
else { echo("<form enctype=multipart/form-data action=?p=upload&w=artist method=POST><table><tr><td><center><b>Song</b></center></td><td><b>Genre</b></td></tr><tr><td><input name=\"uploaded\" type=\"file\" /></td><td><input name=genre type=text>");  }
echo("</td><td><input type=hidden name=form value=upload><input type=submit value=Upload></td></tr></table></form><p><br><p>");


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.