hi everyone :)

i dont know where i am doing it wrong...but cant upload my file...there is no error but its not saving the whole path in DB and not uploading anything in the directroy as well... please point me to riht direction tahnx :)

<?php include("../includes/config.php");?>

<?php
$mediafile = $_FILES['uploadedfile']['name'];
$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4","ppt","avi","mov","moov","qt","flv","FLV");
$extension = end(explode(".", $_FILES["uploadedfile"]["name"]));
if (( ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "audio/mpeg")
|| ($_FILES["uploadedfile"]["type"] == "audio/mp3")
|| ($_FILES["uploadedfile"]["type"] == "video/mp4")
|| ($_FILES["uploadedfile"]["type"] == "video/avi")
|| ($_FILES["uploadedfile"]["type"] == "video/quicktime")
|| ($_FILES["uploadedfile"]["type"] == "video/x-flv")
|| ($_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/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_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/";
$imgdir = "graphics/learningmaterial/";
$imgname = $imgdir.$mediafile;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $imgname))
{
    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."','".$imgname."','".$duration."','".$courseid."','".$type."','".$order."')");
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  else {
    echo header("Location:manage-material.php?status=1&id=".$courseid);
    }
//echo header("Location:manage-material.php?status=1&id=".$courseid);
//echo header ($_FILES['uploadedfile']['type']);
exit();

mysql_close($con);

?>

Dani AI

Generated

Quick summary and focused checklist

The symptoms you reported — var_dump($_FILES) showing a valid upload entry, a “Strict Standards: Only variables should be passed by reference” message, and later an “Undefined variable: imgname” plus the upload silently failing for images/videos — point to two things: (a) a PHP warning from calling end(explode(...)) and (b) your script continuing to the DB insert even when the file move never happened. As @diafol and suggested, fix the reference-by-value warning, then make the upload flow robust and fail-safe before inserting the path into the database.

Priority checklist (do these in order)

  • HTML/form: confirm the form uses method="post" and enctype="multipart/form-data" and the file input name exactly matches the PHP key.
  • Check $_FILES['uploadedfile']['error'] and handle all codes; common values: 0 = OK, 1 = INI_SIZE, 2 = FORM_SIZE, 3 = PARTIAL, 4 = NO_FILE, 6 = NO_TMP_DIR, 7 = CANT_WRITE, 8 = EXTENSION.
  • php.ini limits: confirm upload_max_filesize and post_max_size are large enough and restart the server after changes.
  • Use safe server-side validation: check is_uploaded_file() then detect MIME with finfo_file() (or getimagesize() for images). Do not rely only on client-supplied $_FILES['type'].
  • Use an absolute, writable target path (for example using __DIR__) and ensure the directory exists and the web server user can write to it.
  • Only set $imgname and run the INSERT after move_uploaded_file() returns true. If the move fails, log $_FILES and the PHP error log and return an informative message instead of inserting a null/undefined path.
  • Avoid sending output before header() redirects; use error_log() or a debug page during development.

Extras and best practices

Sanitize and uniquify filenames (timestamp or random token), and switch from deprecated mysql_* to prepared statements (mysqli or PDO). Turn on error_reporting(E_ALL) and check the server error log when things fail. If you still see failures after these steps, post the exact $_FILES var_dump and the numeric error value so the cause can be pinpointed.

Recommended Answers

All 18 Replies

Any errors in the error log?

no there are no errors... :(


its not saving the links and uploading viodes and images only...working fine for audio,ppt,pdf

viodes and images only

Perhaps a 2Mb upload limit?


thanx for response
nop its upload audios of 6 MB even...

but still its not uploading pictures of 500 KB ?? it seems strange
what could be the problem??

Member Avatar for Member #120589

I believe I mentioned using end and explode on the same line here:

http://www.daniweb.com/web-development/php/threads/439050/file-upload-not-working-for-audio-and-video-files#post1887945

You don't seem to have done anything about that. Did you try it?

Does $_FILES['uploadedfile']['error'] give any clue? You could do a var_dump($_FILES) to see all items:

Error codes can be seen here: http://www.php.net/manual/en/reserved.variables.files.php#109648

@diafol i tried it before.. but it didnot gave any error i've used var_dump($_FILES)
and now these are the errors

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\project\teacher\add-material-action.php on line 6
There was an error uploading the file, please try again!
Notice: Undefined variable: imgname in C:\xampp\htdocs\project\teacher\add-material-action.php on line 67
array(1) { ["uploadedfile"]=> array(5) { ["name"]=> string(10) "1 copy.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xampp\tmp\php22B0.tmp" ["error"]=> int(0) ["size"]=> int(208739) } } 

these errors are shown only for videos and images.
it is uploading other media perfectly but with this warning

 Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\project\teacher\add-material-action.php on line 6
Member Avatar for Member #120589

I've mentioned twice now that end and explode shouldn't be combined. Have you tried separating them?

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

I'm assuming that the problem files do have an extension in the first place.

@diafol sorry for bothering you

yes i've tried it aswell this is the error that only removes the strict standards error...
this is the error i encounter after seperating end and explode

There was an error uploading the file, please try again!
Notice: Undefined variable: imgname in C:\xampp\htdocs\project\teacher\add-material-action.php on line 69
array(1) { ["uploadedfile"]=> array(5) { ["name"]=> string(15) "14062012394.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xampp\tmp\php4B46.tmp" ["error"]=> int(0) ["size"]=> int(313467) } } 
Member Avatar for Member #120589

$imgfile is not being set at this point:

$imgname = $imgdir.$mediafile;

If you could do some proper indenting of your code, that would help - nested ifs and no indenting is almost impossible to assimilate.

hi.. Riu 2009,
pls check this code... it may help you...
its working for .doc,.txt,.jpg,.csv... etc....it saves path in database and also
saves files in the directory...
but its not sure about audio and video files...

<?php
    $name           =  $_POST['name'];
    $resumepath     =  $_FILES['resume']['name'];

    $resume=$_FILES['resume']['name'];
    $file=$_FILES['resume']['tmp_name'];
    $ext=strstr($resume,".");
    $resumepath=$name.$ext;
//echo $resumepath;
@move_uploaded_file($file,"../admin/resume/$resumepath"); //file uploading

 $sql="INSERT INTO  tbl_candidate (resume) VALUES ('$resumepath')";
          $obj->query($sql) or die(mysql_error());


        }


        header("Location: ./candidatelist.php");
        exit();
    ?>  

@amiyar thanks for ur help :) i think im so messed up with this code and i think i would have to put a lot of things in the code for my use...

@diafol i know the code is so annoying..sorry about that.any other way to solve this problem??

Member Avatar for Member #120589
<?php

...

$mediafile = $_FILES['uploadedfile']['name'];

$allowedExts = array("jpg", "jpeg", "gif", "png","txt","doc","pdf","mp3","mp4","ppt","avi","mov","moov","qt","flv","FLV");

$extension = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION);

//why check for extension AND type? They're not checked on the server. Use one or t'other.

if (( ($_FILES["uploadedfile"]["type"] == "image/gif")
    || ($_FILES["uploadedfile"]["type"] == "audio/mpeg")
    || ($_FILES["uploadedfile"]["type"] == "audio/mp3")
    || ($_FILES["uploadedfile"]["type"] == "video/mp4")
    || ($_FILES["uploadedfile"]["type"] == "video/avi")
    || ($_FILES["uploadedfile"]["type"] == "video/quicktime")
    || ($_FILES["uploadedfile"]["type"] == "video/x-flv")
    || ($_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/jpg")
    || ($_FILES["uploadedfile"]["type"] == "image/gif")
    || ($_FILES["uploadedfile"]["type"] == "image/png")
    || ($_FILES["uploadedfile"]["type"] == "image/pjpeg")) && in_array(strtolower($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/";
    $imgdir = "graphics/learningmaterial/";
    $imgname = $imgdir.$mediafile;
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $imgname)){
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    }

}else{
    echo "There was an error uploading the file, please try again!";
}


echo (isset($imgname)) ? "IMAGENAME SET!!" : "IMAGENAME NOT SET";

?>
<?php

$p = array_map("mysql_real_escape_string",$_POST);
$title=$p['title'];
$url=$p['url'];
$source=$p['source'];
$description=$p['description'];
$extid=$p['external_id'];
$filename=$p['internalfilename'];
$filesize=$p['internalfilesize'];
$duration=$p['externalduration'];
$courseid=$p['coursesid'];
$type=$p['type'];
$order=$p['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','$imgname','$duration','$courseid','$type','$order')");

if (!mysql_query($sql,$con)){
    die('Error: ' . mysql_error());
}else{
    echo header("Location:manage-material.php?status=1&id=".$courseid);
}
//echo header("Location:manage-material.php?status=1&id=".$courseid);
//echo header ($_FILES['uploadedfile']['type']);
exit();
mysql_close($con);
?>

Not cheked too closely.

@diafol im so sorry for annoying you..i have done everything you said and this is the result

There was an error uploading the file, please try again!IMAGENAME NOT SET
Notice: Undefined variable: imgname in C:\xampp\htdocs\project\teacher\add-material-action.php on line 74
array(1) { ["uploadedfile"]=> array(5) { ["name"]=> string(15) "01072012499.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(24) "C:\xampp\tmp\phpA5E3.tmp" ["error"]=> int(0) ["size"]=> int(245245) } } 
Member Avatar for Member #120589

OK, it's your main IF clause (obviously). Change to just:

if(in_array(strtolower($extension), $allowedExts)){
    ...
}

for now. See if it's the list of types. You're using the 'pathinfo' right?

@diafol yeah using PATHINFO

where should i place this???

Member Avatar for Member #120589
if(in_array(strtolower($extension), $allowedExts)){
    ...
}

instead of:

$extension = end(explode(".", $_FILES["uploadedfile"]["name"]));
if (( ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "audio/mpeg")
|| ($_FILES["uploadedfile"]["type"] == "audio/mp3")
|| ($_FILES["uploadedfile"]["type"] == "video/mp4")
|| ($_FILES["uploadedfile"]["type"] == "video/avi")
|| ($_FILES["uploadedfile"]["type"] == "video/quicktime")
|| ($_FILES["uploadedfile"]["type"] == "video/x-flv")
|| ($_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/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg"))
&& in_array($extension, $allowedExts)){
    ...
}

yup you were right @diafol this line
&& in_array($extension, $allowedExts cause all the problems...

i've removed the extensions for time being and the code is working. Thanx for helping fo so long

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.