Link to home
Start Free TrialLog in
Avatar of tarakwar101
tarakwar101Flag for United States of America

asked on

Warning: move_uploaded_file failed to open stream:

Where should I start here? Not sure why it is doing this this when I attempt to upload an image to display on courses page? all the other images in past that were uploaded still show but soemthing is now not workign?

Warning: move_uploaded_file(../courses/course_id145/image/certgeorgia.gif): failed to open stream: No such file or directory in C:\Data\mysite\admin\course_new.php on line 174 Warning: move_uploaded_file(): Unable to move 'C:\php\uploadtemp\php3D3E.tmp' to '../courses/course_id145/image/certgeorgia.gif' in C:\Data\mysite\admin\course_new.php on line 174 Warning: Cannot modify header information - headers already sent by (output started at C:\Data\mysite\admin\course_new.php:174) in C:\Data\mysite\admin\course_new.php on line 211
Avatar of Aamir Saeed
Aamir Saeed
Flag of Pakistan image

It would be easy if you paste your code here.
Avatar of tarakwar101

ASKER

You sure about that? It's alot of code!

Here is the PHP code, also note: I am using a smarty template, so one page has the php and the other is the html.

PHP code:

<?php
      require("defs.php");
      require("defs_img.php");
      require("common.php");

      if (isset($session_data)) unset($session_data);
      session_start();
      session_register("session_data");
      $cid = GetConnectionID();
      if (!$cid)
      {
            echo "Cannot connect to MySQL";
            return;
      }

      if (!isset($id)) return;

      $errors = array();
      if (isset($submit))
      {
            $ptn_time = "/^\s*(\d+)\s*$/";
            if (!isset($subj) || !trim($subj))
                  $errors[] = "'Course name' is empty";
            if (!isset($descr) || !trim($descr))
                  $errors[] = "'Course description' is empty";
            if ($timelimit=='Y')
            {
                  if (!isset($time) || !trim($time))
                        $errors[] = "'Time for reading course' is empty";
                  elseif (isset($time) && $time && !preg_match($ptn_time, $time))
                        $errors[] = "'Time for reading course' is incorrect";
                  elseif ($time == 0)
                        $errors[] = "'Time for reading course' must be greather than 0";
            }

            if (isset($cost) && $cost && !preg_match($ptn_time, $cost))
                  $errors[] = "'Price' is incorrect";
            if (isset($corpcosttwo) && $corpcosttwo && !preg_match($ptn_time, $corpcosttwo))
                  $errors[] = "'Corporate Price 20%' is incorrect";
            if (isset($corpcosttwofive) && $corpcosttwofive && !preg_match($ptn_time, $corpcosttwofive))
                  $errors[] = "'Corporate Price 25%' is incorrect";
            if (isset($corpcostthree) && $corpcostthree && !preg_match($ptn_time, $corpcostthree))
                  $errors[] = "'Corporate Price 30%' is incorrect";
            if (isset($corpcostfour) && $corpcostfour && !preg_match($ptn_time, $corpcostfour))
                  $errors[] = "'Corporate Price 40%' is incorrect";
            if (isset($corpcostpre_two) && $corpcostpre_two && !preg_match($ptn_time, $corpcostpre_two))
                  $errors[] = "'Pre-Paid Corporate Price 20%' is incorrect";
            if (isset($corpcostpre_twofive) && $corpcostpre_twofive && !preg_match($ptn_time, $corpcostpre_twofive))
                  $errors[] = "'Pre-Paid Corporate Price 25%' is incorrect";
            if (isset($corpcostpre_three) && $corpcostpre_three && !preg_match($ptn_time, $corpcostpre_three))
                  $errors[] = "'Pre-Paid Corporate Price 30%' is incorrect";
            if (isset($corpcostpre_four) && $corpcostpre_four && !preg_match($ptn_time, $corpcostpre_four))
                  $errors[] = "'Pre-Paid Corporate Price 40%' is incorrect";


      $endDate = mktime(23,59,59,$endMonth,$endDay,$endYear);
      if ($endDate <= mktime())
            $errors[] = "'Expiration date' is earlier than now date";

            if ($timetestlimit=='Y')
            {
                  if (!isset($timetest) || !trim($timetest))
                        $errors[] = "'Time for passing the test' is empty";
                  elseif (isset($timetest) && $timetest && !preg_match($ptn_time, $timetest))
                        $errors[] = "'Time for passing the test' is incorrect";
                  elseif ($timetest == 0)
                        $errors[] = "'Time for passing the test' must be greather than 0";
            }

            if ($rndtest=='Y')
            {
                  if (!isset($qcount) || !trim($qcount))
                        $errors[] = "'Number of questions' is empty";
                  elseif (isset($qcount) && $qcount && !preg_match($ptn_time, $qcount))
                        $errors[] = "'Number of questions' is incorrect";
                  elseif ($qcount == 0)
                        $errors[] = "'Number of questions' must be greather than 0";
            }
            if ($regardless=='N')
            {
                  if (!isset($reqscore) || !trim($reqscore))
                        $errors[] = "'Required passing score' is empty";
                  elseif (isset($reqscore) && $reqscore && !preg_match($ptn_time, $reqscore))
                        $errors[] = "'Required passing score' is incorrect";
                  elseif ($reqscore == 0)
                        $errors[] = "'Required passing score' must be greather than 0";
            }

            if (is_uploaded_file($image))
            {
                  $badfile = true;
                  if (isset($image_type))
                  {
                        $type = substr($image_type,0,5);
                        if ($type=="image")
                              $badfile=false;
                  }
                  if ($badfile)
                  {
                        $errors[] = "Uploaded file is not image";
                        $image="";
                  }
            }

            if (count($errors) == 0)
            if ($id)
            {
                  if (isset($delimg) && ($delimg=="on"))
                  {
                        $query2 = "SELECT Image FROM COURSE WHERE CourseID=$id";
                        $result = mysql_query($query2, $cid);
                        if ($result)
                        {
                              $image2 = mysql_result($result, "Image");
                              $IMAGE_LOCATION = GetCourseImageFolder($id, $subj)."/".$image2;
                              if ( is_file($IMAGE_LOCATION) )
                                    unlink( $IMAGE_LOCATION );
                        }
                        $query2 = "UPDATE COURSE SET Image=NULL WHERE CourseID=$id";
                        mysql_query($query2, $cid);
                  }

                  $query = "SELECT Subj FROM COURSE WHERE CourseID=$id";
                  $result = mysql_query($query, $cid);

                  if ($result)
                  {
                        $subj_old = mysql_result($result, "Subj");
                        $COURSE_LOCATION_OLD = GetCourseFolder($id, $subj_old);
                        $COURSE_LOCATION = GetCourseFolder($id, $subj);
                        if ((is_dir($COURSE_LOCATION_OLD))
                           && ($COURSE_LOCATION_OLD !== $COURSE_LOCATION))
                              rename($COURSE_LOCATION_OLD, $COURSE_LOCATION);
                  }

                  $query = "UPDATE COURSE SET Number=\"".addslashes($number)."\", Subj=\"".addslashes($subj)."\",
                        Author=\"".addslashes($aname)."\", Author=\"".addslashes($ccode)."\", Descr=\"".addslashes($descr)."\",
                        TimeLimit=\"$timelimit\"";
                  if ($timelimit=='Y')
                        $query .= ", Time=\"".$time."\"";
                  else
                        $query .= ", Time=0";
                        $query .= ", QuizCount=\"$quizcount\", Cost=\"$cost\", CorpCosttwo=\"$corpcosttwo\", CorpCosttwofive=\"$corpcosttwofive\", CorpCostthree=\"$corpcostthree\", CorpCostfour=\"$corpcostfour\", CorpCostpre_two=\"$corpcostpre_two\", CorpCostpre_twofive=\"$corpcostpre_twofive\", CorpCostpre_three=\"$corpcostpre_three\", CorpCostpre_four=\"$corpcostpre_four\", Shipping_Cost=\"$shipping_cost\", TimeTestLimit=\"$timetestlimit\"";
                  if ($timetestlimit=='Y')
                        $query .= ", TimeTest=\"".$timetest."\"";
                  else
                        $query .= ", TimeTest=0";
                  $query .= ", RndTest='$rndtest'";
                  if ($rndtest=='Y')
                        $query .= ", QuestCount=\"".$qcount."\"";
                  else
                        $query .= ", QuestCount=0";
                  $query .= ", Redirect='$redirect', RedIncor='$redincor', Regardless=\"$regardless\"";
                  if ($regardless=='N')
                        $query .= ", ReqScore=\"$reqscore\"";
                  else
                        $query .= ", ReqScore=0";
                  $query .= ", KillDate=\"".mktime(23,59,59,$endMonth,$endDay,$endYear)."\"";

                  if ( is_uploaded_file($image) )
                  {
                        $query2 = "SELECT Image FROM COURSE WHERE CourseID=$id";
                        $result = mysql_query($query2, $cid);

                        if ($result )
                        {
                              $image2 = mysql_result($result, "Image");
                              if ( is_file($image2) )
                                    unlink( GetCourseImageFolder($id, $subj)."/$image2" );
                        }
                        $IMAGE_LOCATION = GetCourseImageFolder($id, $subj)."/".$HTTP_POST_FILES["image"]["name"];
                        $query .= ", Image=\"".$HTTP_POST_FILES["image"]["name"]."\"";
                        move_uploaded_file($image, $IMAGE_LOCATION);
                  }
                  $query .= " WHERE CourseID=$id";
                  mysql_query($query, $cid);
            }
            else
            {
                  $query = "INSERT INTO COURSE VALUES ($id, \"".addslashes($number)."\", \"".addslashes($subj)."\",
                        \"".addslashes($aname)."\", \"".addslashes($ccode)."\", \"".addslashes($descr)."\", \"$timelimit\",
                        \"".(isset($time)?$time:'0')."\", \"$quizcount\", \"$cost\", \"$corpcosttwo\", \"$corpcosttwofive\", \"$corpcostthree\", \"$corpcostfour\",
                        \"$corpcostpre_two\", \"$corpcostpre_twofive\", \"$corpcostpre_three\", \"$corpcostpre_four\", \"$shipping_cost\", \"USD\", NULL, \"$timetestlimit\",
                        \"".(isset($timetest)?$timetest:'0')."\", '$rndtest', \"".(isset($qcount)?$qcount:'0')."\",
                        '$redirect', '$redincor', \"$regardless\", \"".(isset($reqscore)?$reqscore:'0')."\",
                        '".($show=='a'?'Y':'N')."', '".mktime()."', \"".mktime(23,59,59,$endMonth,$endDay,$endYear)."\")";
                  
                  $result = mysql_query($query, $cid);
                  if ($result)
                  {
                        $id = mysql_insert_id($cid);
                        $COURSE_LOCATION = GetCourseFolder($id, $subj);
                        mkdir($COURSE_LOCATION, 0777);
                        $IMAGE_LOCATION = GetCourseImageFolder($id, $subj);
                        mkdir($IMAGE_LOCATION, 0777);
                        mkdir($COURSE_LOCATION."/lessons", 0777);
                        if ( is_uploaded_file($image) )
                        {
                              $IMAGE_LOCATION .= "/".$HTTP_POST_FILES["image"]["name"];
                              move_uploaded_file($image, $IMAGE_LOCATION);
                              $query = "UPDATE COURSE SET Image=\"".$HTTP_POST_FILES["image"]["name"]."\" WHERE CourseID=$id";
                              mysql_query($query, $cid);
                        }
                  }
            }
            if (count($errors) == 0)
            {
                  header("LOCATION: course_ed.php?show=$show");
                  exit;
            }
      }
      if (count($errors) == 0)
      {
            $number = $subj = $aname = $ccode = $descr = $image = $time = $quizcount = $cost = $corpcosttwo = $corpcosttwofive = $corpcostthree = $corpcostfour = $corpcostpre_two = $corpcostpre_twofive = $corpcostpre_three = $corpcostpre_four = $shipping_cost = $timetest = $qcount = $reqscore = "";
            $timelimit = $timetestlimit = $rndtest = $redincor = 'N';
            $redirect = $regardless = 'Y';

            if ($id)
            {
                  $QUERY_SELECT = "SELECT * FROM COURSE WHERE CourseID=".$id;
                  $result = mysql_query($QUERY_SELECT, $cid);
                  $row = mysql_fetch_array($result);
                  $number            = $row["Number"];
                  $subj            = stripslashes($row["Subj"]);
                  $aname            = stripslashes($row["Author"]);
                  $ccode            = stripslashes($row["CourseCode"]);
                  $descr            = stripslashes($row["Descr"]);
                  $image            = stripslashes($row["Image"]);
                  $timelimit      = $row["TimeLimit"];
                  $time            = $row["Time"];
                  $quizcount      = $row["QuizCount"];
                  $cost            = $row["Cost"];
                  $corpcosttwo  = $row["CorpCosttwo"];
                  $corpcosttwofive  = $row["CorpCosttwofive"];
                  $corpcostthree      = $row["CorpCostthree"];
                  $corpcostfour      = $row["CorpCostfour"];
                  $corpcostpre_two      = $row["CorpCostpre_two"];
                  $corpcostpre_twofive      = $row["CorpCostpre_twofive"];
                  $corpcostpre_three      = $row["CorpCostpre_three"];
                  $corpcostpre_four      = $row["CorpCostpre_four"];
                  $shipping_cost      = $row["Shipping_Cost"];
                  
                  if (isset($row["Image"]) && $row["Image"])
                        $image = GetCourseImageFolder($id, $subj)."/".$row["Image"];
                  else
                        $image = "";
                  $timetestlimit      = $row["TimeTestLimit"];
                  $timetest      = $row["TimeTest"];
                  $rndtest      = $row["RndTest"];
                  $qcount            = $row["QuestCount"];
                  $redirect      = $row["Redirect"];
                  $redincor      = $row["RedIncor"];
                  $regardless      = $row["Regardless"];
                  $reqscore      = $row["ReqScore"];
                  $endDate      = $row["KillDate"];
                  $endDay            = date("d", $endDate);
                  $endMonth      = date("m", $endDate);
                  $endYear      = date("Y", $endDate);
            }
            else
            {
                  $endDay = date("d");
                  $endMonth = date("m");
                  $endYear = date("Y")+1;
            }
      }
      if ($id)
      {
            $smarty->assign("status1", "EDIT COURSE ");
            $smarty->assign("status2", "\"$subj\"");
      }
      else
      {
            $smarty->assign("status1", "EDIT COURSES | ");
            $smarty->assign("status2", "ADD NEW COURSE");
      }

      if (count($errors) && !isset($qcount))
            $qcount="";
      if (count($errors) && !isset($reqscore))
            $reqscore="";
      if (count($errors) && !isset($time))
            $time="";
      if (count($errors) && !isset($timetest))
            $timetest="";

      $course = array();
      $course["id"] = $id;
      $course["number"] = $number;
      if (isset($show))
            $course["show"] = $show;
      else
            $course["show"] = "";

      $course["name"] = $subj;
      $course["aname"] = $aname;
      $course["ccode"] = $ccode;
      $course["description"] = $descr;
      $course["quizcount"] = $quizcount;

      if ($image && $image != "none")
      {
            $course["image"] = $image;
            $image_size = GetNewSize($image);
            $course["image_width"] = $image_size["width"];
            $course["image_height"] = $image_size["height"];
      }
      else
      {
            $course["image"] = "";
            $course["image_width"] = "";
            $course["image_height"] = "";
      }

      $timelim = array();
      $timelim["values"] = array('Y','N');
      $timelim["selected"] = $timelimit;
      $timelim["output"] = array("yes","no");

      $course["time"] = $time;
      $course["quizcount"] = $quizcount;
      $course["price"] = $cost;
      $course["corpcosttwo"] = $corpcosttwo;
      $course["corpcosttwofive"] = $corpcosttwofive;
      $course["corpcostthree"] = $corpcostthree;
      $course["corpcostfour"] = $corpcostfour;
      $course["corpcostpre_two"] = $corpcostpre_two;
      $course["corpcostpre_twofive"] = $corpcostpre_twofive;
      $course["corpcostpre_three"] = $corpcostpre_three;
      $course["corpcostpre_four"] = $corpcostpre_four;
      $course["shipping_cost"] = $shipping_cost;
      $course["endDate"]  = mktime(23,59,59,$endMonth,$endDay,$endYear);

      $timelimt = array();
      $timelimt["values"] = array('Y','N');
      $timelimt["selected"] = $timetestlimit;
      $timelimt["output"] = array("yes","no");

      $course["timetest"] = $timetest;

      $random = array();
      $random["values"] = array('Y','N');
      $random["selected"] = $rndtest;
      $random["output"] = array("yes","no");

      $course["number_of_question"] = $qcount;

      $red = array();
      $red["values"] = array('Y','N');
      $red["selected"] = $redirect;
      $red["output"] = array("yes","no");

      $redinc = array();
      $redinc["values"] = array('Y','N');
      $redinc["selected"] = $redincor;
      $redinc["output"] = array("yes","no");

      $regless = array();
      $regless["values"] = array('Y','N');
      $regless["selected"] = $regardless;
      $regless["output"] = array("yes","no");

      $course["reqscore"] = $reqscore;

      $js = "
 var ptn_empty=/^\s*$/;
 var ptn_positiv_digit=/^\s*(\d+)\s*$/;

 function check_empty(field, name)
 {
      if (!field.value || ptn_empty.test(field.value))
      {
            error = \"The field '\"+name+\"' is empty. Fill it, please.\";
            alert(error); field.focus(); field.select();
            return false;
      }
      return true;
 }

 function check(field, pattern, name)
 {
      if (field.value && pattern.test(field.value) == false)
      {
            error = \"The field '\"+name+\"' is incorrect. Change it, please.\";
            alert(error); field.focus(); field.select();
            return false;
      }
      return true;
 }
 function checklen(field)
 {
      if (field.value.length > 200)
            field.value=field.value.substring(0, 200);
 }
 function check_courses()
 {
      res = check_empty(data.subj, 'Course name')
         && check_empty(data.descr, 'Description on first page')
         && check(data.cost, ptn_positiv_digit, 'Price');

      if (res==false)      return false;
      if ((data.timelimit.value=='Y')||(!data.timelimit.value))
      {
            if (check_empty(data.time, 'Time for reading course')==false)
                  return false;
            else if (check(data.time, ptn_positiv_digit, 'Time for reading course')==false)
                  return false;
            else if (data.time.value == 0)
            {
                  alert('Time for reading course must be greather than 0');
                  return false;
            }
      }
      if ((data.timetestlimit.value=='Y')||(!data.timetestlimit.value))
      {
            if (check_empty(data.timetest, 'Time for passing the test')==false)
                  return false;
            else if (check(data.timetest, ptn_positiv_digit, 'Time for passing the test')==false)
                  return false;
            else if (data.timetest.value == 0)
            {
                  alert('Time for passing the test must be greather than 0');
                  return false;
            }
      }
      if ((data.rndtest.value=='Y')||(!data.rndtest.value))
      {
            if (check_empty(data.qcount, 'Number of question')==false)
                  return false;
            else if (check(data.qcount, ptn_positiv_digit, 'Number of question')==false)
                  return false;
            else if (data.qcount.value == 0)
            {
                  alert('Number of questions must be greather than 0');
                  return false;
            }
      }
      if ((data.regardless.value=='N')||(!data.regardless.value))
      {
            if (check_empty(data.reqscore, 'Required passing score')==false)
                  return false;
            else if (check(data.reqscore, ptn_positiv_digit, 'Required passing score')==false)
                  return false;
            else if (data.reqscore.value == 0)
            {
                  alert('Required passing score must be greather than 0');
                  return false;
            }
      }      
 }
";
      $smarty->assign("js",$js);
      $smarty->assign("course",$course);
      $smarty->assign("timelim",$timelim);
      $smarty->assign("timelimt",$timelimt);
      $smarty->assign("random",$random);
      $smarty->assign("red",$red);
      $smarty->assign("redinc",$redinc);
      $smarty->assign("regless",$regless);
      $smarty->assign("errors",$errors);
      $smarty->display("course_new.tpl");
?>
======================================================================

The HTML code:

{include file="site_header.tpl"}
{include file="status_bar.tpl"}

<tr>
      <td>
       <font class="text" id="clr_red">
        &nbsp;* - required fields<br></font>
      </td>
</tr>
<TR>
        <TD>
        {section name=c loop=$errors}
                <FONT CLASS=text>{$errors[c]}
                <BR>
        {/section}
        </TD>
</TR>
<TR>
        <TD>
        <FORM NAME="data" onSubmit="return check_courses()" ACTION="course_new.php" METHOD="post" ENCTYPE="multipart/form-data">
        <INPUT TYPE="hidden" NAME="id" VALUE={$course.id}>
        {if $course.show}
                <INPUT TYPE="hidden" NAME="show" VALUE={$course.show}>
        {/if}

        <TABLE BORDER=0 BGCOLOR=#E6E6E6 CELLSPACING=2 CELLPADDING=3 STYLE="WIDTH:80%">
        <TR><TH WIDTH=30%></TH><TH WIDTH=70%></TH></TR>

        <!-- course number -->

      <TR BGCOLOR=#FFFFFF>
      <TD><FONT CLASS=text>Course number</FONT></TD>
      <TD><INPUT NAME="number" CLASS=wide TYPE=text VALUE="{$course.number}">

        <!-- course name -->

      <TR BGCOLOR="#FFFFFF">
        <TD><FONT CLASS=text>Course name<font class="text" id="clr_red"> * </font></TD>
        <TD>
        <TEXTAREA onkeyup="javascript:checklen(this.form.subj)" NAME="subj" ROWS=2>{$course.name}</TEXTAREA>

        <!-- course author -->

      <TR BGCOLOR=#FFFFFF>
      <TD><FONT CLASS=text>Course Description-html link</FONT></TD>
      <TD><INPUT NAME="aname" CLASS=wide TYPE=text VALUE="{$course.aname}">
          <!-- Course Code -->
            <TR BGCOLOR=#FFFFFF>
      <TD><FONT CLASS=text>Course Code</FONT></TD>
      <TD><INPUT NAME="ccode" CLASS=wide TYPE=text VALUE="{$course.ccode}">

        <!-- course description -->

      <TR BGCOLOR="#FFFFFF">
        <TD><FONT CLASS=text>Course description on Courses page<font class="text" id="clr_red"> * </font></TD>
        <TD><TEXTAREA NAME="descr" ROWS="10">{$course.description}</TEXTAREA>

        <!-- course image -->

        <TR BGCOLOR="#FFFFFF">
        <TD><FONT CLASS=text>Image</TD>
        <TD>
        {if $course.image}
                <IMG SRC='{$course.image}' WIDTH='{$course.image_width}' HEIGHT='{$course.image_height}'>
                <BR><INPUT CLASS=rb TYPE=checkbox NAME="delimg"><FONT CLASS=text>Delete image
        {/if}
        <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="2000000">
        <INPUT TYPE="hidden" NAME="old_image" VALUE="{$course.image}">
        <INPUT CLASS="wide" TYPE="file" NAME="image">

======took out all the middle code, to make smaller============================================================


        <TR BGCOLOR="#E6E6E6">
        <TD>&nbsp;</TD>
        <TD ALIGN="left">
        <INPUT TYPE="submit" NAME="submit" VALUE="Save" STYLE="BACKGROUND-COLOR:#738CB6"></td>
      
              <TR></TR><TD></TD>
              <TR></TR><TD></TD>
              <TR></TR><TD></TD>

{include file="site_footer.tpl"}
<script language="JavaScript">
<!--
        {$js}
-->
</script>
I figured it out myself, it was not creating the folders to put the image in on submit.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial