Page 1 of 1

Please someone help me

Posted: Sat Mar 27, 2004 8:07 am
by phpnovice
Ok im trying to upload small .wav files to a BLOB in a mysql database from a form in php.

The form is like so:

<form method ="post" action="/~bj253/PROJECT/PRJuploadfile.php" enctype="multipart/form-data">
<table><tr><td>Select a .wav file to upload</td><td><input type="file" name="userfile"></td></tr>
<tr><td><input type="submit" value="Upload"></td></tr></table></form>

and then PRJuploadfile.php goes like this:

Code: Select all

<?php
Temporary name: <?php echo $_FILE['userfile']['tmp_name'] ?><br />
Original name: <?php echo $_FILE['userfile']['name'] ?><br />
Size: <?php echo $_FILE['userfile']['size'] ?> bytes<br />
Type: <?php echo $_FILE['userfile']['type'] ?>

<?
//include to check if user is logged in (malicous users may try to type in url to skip login)
include 'secrets.php';
//include details for logging onto mysql database
include 'blah.php';
//include error function
include 'blah.php';
error_reporting(E_ALL);


//an error string
$errorstring="";


//check the file to see if there it is compatible
if ( !ereg( ".wav", $_FILES['userfile']['type']) ) {
	$errorstring.="\n<br>This is not a valid file type";
}else if ($_FILES['userfile']['size'] > 300000 ) {
   $errorstring.="\n<br>Sorry file too large";
} else if ( !($connection=mysql_connect($host, $user, $passwd)) ) {
   $errorstring.="\n <br>Error connecting to database";
} else if ( !(mysql_select_db($dbName,$connection)) ) {
   $errorstring.="\n <br>Error selecting database";
} else if ( !($handle = fopen ($_FILES['userfile']['tmp_name'], "r")) ) {
   $errorstring.="\n<br>Error opening temp file";
} else if ( !($mp3 = fread ($handle, filesize($_FILES['userfile']['tmp_name']))) ) {
   $errorstring.="\n<BR>Error reading temp file";
}
else{
   $ext = explode('.', $_FILES['userfile']['name']);
   $userfile = $_SESSION["user_id"] . '.' . $ext[count($ext)-1];
   fclose ($handle);
  // $mp3 = mysql_escape_string($mp3);
   $query = "INSERT INTO TEMP(user_id, track_id, temp_userfile, temp_binary) VALUES ('" . $_SESSION["user_id"] . "','" . $_SESSION["track_id"] . "','" . $userfile . "','" . $mp3 . "')";
   print $query;
   if ( !(mysql_query($query,$connection)) ) {
      $errorstring .="\n <br> Error writing wav to database";
   } else {
      $errorstring .="\n <br>wav successfully copied to database";
   }

print $errorstring;
}
?>
i just keep getting parse errors and i dont get to see any of the print statements. im uploading a test file of 350kb but its not working for BLEEP!

Any help would be helpful.

James

Posted: Sat Mar 27, 2004 8:12 am
by Joe
If you are only using one result for each if/else is statement then there is no need for '{' parses.

TRY:
if ( !ereg( ".wav", $_FILES['userfile']['type']) )
$errorstring.="\n<br>This is not a valid file type";
else if ($_FILES['userfile']['size'] > 300000 )
$errorstring.="\n<br>Sorry file too large";
else if ( !($connection=mysql_connect($host, $user, $passwd)) )
$errorstring.="\n <br>Error connecting to database";
else if ( !(mysql_select_db($dbName,$connection)) )
$errorstring.="\n <br>Error selecting database";
else if ( !($handle = fopen ($_FILES['userfile']['tmp_name'], "r")) )
$errorstring.="\n<br>Error opening temp file";
else if ( !($mp3 = fread ($handle, filesize($_FILES['userfile']['tmp_name']))) )
$errorstring.="\n<BR>Error reading temp file";


I think im right there, but i may be wrong!

Posted: Sat Mar 27, 2004 8:33 am
by phpnovice
oh that helped a bit, i ahem "borrowed" the code from one of our lectures, but my lecturer seems to do things to hard way sometimes. now it is passing that in and i can see the contents of the query are correct but im now getting a mysql errorno 1064: cant remember which one that is. Any ideas, bearing in mind all the if statements arent parsed with "{" anymore.

Jim

Posted: Sat Mar 27, 2004 9:19 am
by phpnovice
ok its cause the track_id isnt passing but i can see why not, i am querying the data base like so:

Code: Select all

<?php
//connection stuff up here
//then this

//get the track_id of the one we just inserted so new query
$query2="select track_id, track_upload_time from TRACK where user_id ='" . $_SESSION["user_id"] ."' order by track_upload_time limit 1";
//run query with some error handling
if(!(@ mysql_query($query2,$connection)))
showerror(); 

$row = mysql_fetch_array($query2);
$_SESSION["track_id"] = $row["track_id"];

print $row;

?>
And this now gives me an error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bj253/public_html/PROJECT/PRJupload.php on line 35

im not sure why this doesnt work cause i have used a similar statement before on the same config so should be working! (they have just turned off global variables)

Posted: Sat Mar 27, 2004 9:31 am
by markl999
Try...

Code: Select all

$query2="select track_id, track_upload_time from TRACK where user_id ='" . $_SESSION["user_id"] ."' order by track_upload_time limit 1";
//run query with some error handling
$result = mysql_query($query2) or die(mysql_error());

$row = mysql_fetch_array($result);
$_SESSION['track_id'] = $row['track_id'];

print_r($row);