hi php geniuses :)
a simple question for u ppl here's my code and i want to upload all files i.e. images, videos, audios, ppt, pdf etc but here ican only upload the images successfully.. when i upload audio file it gives following error. please check my code and suggest a solution tahnx in advance :)

this is add-material.php

<div class="span-18 last">
    <h2 class="alt">Add Material</h2>
<form id="form" method="post" action="add-material-action.php" enctype="multipart/form-data">
<label for="title">Title:</label><input  type="text" name="title" id="title" class="text "value="" /><br />
<label for="url">Url:</label><input type="text" name="url" id="url" class="text" value="" /><br />
<label>Source:</label> 
<input type="radio" name="source" value="E" checked />External
<input type="radio" name="source" value="I" />Internal<br /><br />
<label for="description">Course Description:</label><br /><textarea name="description" id="description"></textarea> <br />
<label for="external_id">External Id:</label><input type="text" class="text" name="external_id" value="" /><br /><br />
<label for="file">Upload Your File Here:</label><input type="file" name="uploadedfile" id="uploadedfile"/><br /><br />
<label for="internalfilename">Internal File Name:</label><input type="text" class="text" name="internalfilename" value="" /><br /><br />
<label for="internalfilesize">File Size (for internal files only):</label><input type="text" name="internalfilesize" class="text" value="" /><br /><br />
<label for="externalduration">Duration (Only for externalfiles):</label><input type="text" name="externalduration" class="text" value="" /><br /><br />
<label for="type">Type:</label>
<select name="type" value="<?php echo $type; ?>">
<optgroup label="VIDEO">
<option value="youtube video">youtube video</option>
<option value="vimeo video">vimeo video</option>
<option value="Dailymotion video">Dailymotion video</option>
</optgroup>
<optgroup label="AUDIO">
<option value="youtube audio">Youtube Audio</option>
</optgroup>
<optgroup label="Images">
<option value="Flickr">Flickr</option>
<option value="Picasa">Picasa</option>
<option value="Slide Share">Slide Share</option>
</optgroup>
<optgroup label="PDF">
<option value="Online Documents">Online Documents</option>
</optgroup>
<optgroup label="Internal Files">
<option value="image">Image</option>
<option value="audio">Audio</option>
<option value="video">Video</option>
<option value="ppt">Presentation</option>
</optgroup>
</select>
<br />
<br />
<label>Choose Course:</label>
<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db($dbname, $con);


$result = mysql_query("SELECT * FROM courses WHERE(accountsid='".$id."')");
echo "<select name='coursesid'>\n";
while($row = mysql_fetch_array($result))
  {
  echo "<option value='".$row['id'] . "'";
  if ($_POST["courseid"]==$row['id']) 
    echo " selected='selected'";
  echo ">" . $row['title'] . "</option>\n";
  }
echo "</select>\n";
mysql_close($con);
?>
<br />
<br />
<label>Order:</label>
<select type="text" name="order" value="<?php echo $order; ?>" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<br />
<br />
<input class="mybutton" type="submit" name="Add Material" class="button"  value="Add Material" />
</form>

</div>

and this is my add-material-action.php

<?php

$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4"."ppt");
$extension = end(explode(".", $_FILES["uploadedfile"]["name"]));
if (( ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "audio/mpeg")
|| ($_FILES["uploadedfile"]["type"] == "video/mp4")
|| ($_FILES["uploadedfile"]["type"] == "application/msword")
|| ($_FILES["uploadedfile"]["type"] == "application/pdf")
|| ($_FILES["uploadedfile"]["type"] == "application/vnd.ms-powerpoint")
|| ($_FILES["uploadedfile"]["type"] == "text/plain")
|| ($_FILES["uploadedfile"]["type"] == "application/pdf")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg"))
&& in_array($extension, $allowedExts)) 
{
 if ($_FILES["uploadedfile"]["error"] > 0)
 {
 echo "Return Code: " . $_FILES["uploadedfile"]["error"] . "<br />";
 }

 elseif (file_exists("graphics/learningmaterial/" . $_FILES["uploadedfile"]["name"]))
  {
  echo $_FILES["uploadedfile"]["name"] . " already exists. ";
  }

$target_path = "graphics/learningmaterial/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
}
}
else
{
    echo "There was an error uploading the file, please try again!";
}

?>

<?php
$title=$_POST['title'];
$url=$_POST['url'];
$source=$_POST['source'];
$description=$_POST['description'];
$extid=$_POST['external_id'];
$filename=$_POST['internalfilename'];
$filesize=$_POST['internalfilesize'];
$duration=$_POST['externalduration'];
$courseid=$_POST['coursesid'];
$type=$_POST['type'];
$order=$_POST['order'];


$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$sql=("INSERT INTO learningmaterial VALUES (NULL,'".$title."', '".$url."','".$source."','".$description."','".$extid."','".$filename."','".$filesize."','".$target_path."','".$duration."','".$courseid."','".$type."','".$order."')");
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo header("Location:manage-material.php?status=1&id=".$courseid);
exit();

mysql_close($con);

?>

Recommended Answers

All 10 Replies

just forgot to paste the error, sorry about that.. this is the error it gives

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\project\teacher\add-material-action.php on line 6
The file abc.mp3 has been uploaded Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near abc.mp3','4 mins','17','audio','2')' at line 1
Member Avatar for diafol
$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4"."ppt");

should be:

$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4","ppt");

Notice the period/comma confusion before the ppt?

Also before checking the nature of $_FILES[...] vars, make sure they exist first.

thanx for ur response @diafol
sir, its again saying the same thing...same code is working perfectly for images...but not for audio.. i couldnot get the reason...

how would i check that they exixt???

Member Avatar for diafol
($_FILES["uploadedfile"]["type"] == "audio/mpeg")

should be

($_FILES["uploadedfile"]["type"] == "audio/mp3")

@diafol i've changed it but the results are same...

i've added some snapshots to clarify

Member Avatar for diafol

It seems that your errors stem from $title not existing and subjectid. Have you checked to see that the file is actually uploaded? Just because it doesn't appear in the DB, doesn't mean that the upload didn't work.

The SQL may be easier to deconstruct with the SET syntax as opposed to VALUES.

When you upload an file, do a "strategic echo", e.g.

echo $_FILES['uploadedfile']['type'];

In addition, it may be useful to do a

print_r($_POST)

to see what you get

@diafol
sir, u r right its uploading the files..i've checked in the folder all the files are there... but its not saving anything database... what should i do then???

@diafol

its giving error on line 6 i.e
$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4"."ppt");

and the error is Strict Standards: Only variables should be passed by reference

Member Avatar for diafol

Are you sure that this is the line that's causing the problem?

In order to get the file extension, use

$extension = pathinfo($filename, PATHINFO_EXTENSION);

And have you changed:

$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4"."ppt");

to

$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4","ppt");

You've posted the same mistake again.

WRT to the error, it's probably due to the end() leading to possible mem leaks. I'd use pathinfo, but if you really MUST use end(), you split the functions:

$bits = explode(".", $_FILES["uploadedfile"]["name"]);
$extension = end($bits);

@diafol i just copied the line from my first post.. yeah im sure i've changed everything + i've added more extensions as well...

now i will try the same.. thnk you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.