0

I am trying to upload an image to a directory and post its file path along with other data into a table. Upon submit it echos Error uploading file, but i cant figure out where its getting hung up. I believe it may have to do with my result variable but I'm not sure why. Thank you for your help.

<!-- Updated -->
    <?PHP
    error_reporting(E_ALL); ini_set('display_errors', 1);
    $hostname = "localhost:3306"; 
    $db_user = "root"; 
    $db_password = "admin"; 
    $database = "smlc"; 
    $db_table = "program";
    $db = mysqli_connect($hostname, $db_user, $db_password);
          mysqli_select_db($db, $database);
    	  
    $uploadDir=dirname(__FILE__)."/images/uploaded/programs/";

    if(isset($_POST['Submit']))
    {
    $program_name = $_POST['program_name'];
    $program_description = $_POST['program_description'];
    $fileName = $_FILES['Photo']['name'];
    $tmpName = $_FILES['Photo']['tmp_name'];
    $fileSize = $_FILES['Photo']['size'];
    $fileType = $_FILES['Photo']['type'];
    $filePath = $uploadDir . $fileName;
    $result = move_uploaded_file($tmpName,$filePath);
    if (!$result) {
    echo "Error uploading file";
    exit;
    }
    if(!get_magic_quotes_gpc())
    {
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
    }

    $sql_program = "INSERT INTO program (program_name, program_description,filepath) 
    VALUES ('$program_name', '$program_description','$filePath')";


    mysqli_query($sql_program) or die('Error, query failed ');
    }
    ?>

<!--image_programs.php-->
  

      <?PHP
        $hostname = "localhost"; 
        $db_user = "*"; 
        $db_password = "*"; 
        $database = "smlc"; 
        $db_table = "program";
        $db = mysql_connect($hostname, $db_user, $db_password);
              mysql_select_db($database);
        	  
        $uploadDir = 'images/Uploaded/programs/'; 
    
        if(isset($_POST['Submit']))
        {
        $program_name = $_POST['program_name'];
        $program_description = $_POST['program_description'];
        $fileName = $_FILES['Photo']['name'];
        $tmpName = $_FILES['Photo']['tmp_name'];
        $fileSize = $_FILES['Photo']['size'];
        $fileType = $_FILES['Photo']['type'];
        $filePath = $uploadDir . $fileName;
        $result = move_uploaded_file($tmpName,$filePath);
        if (!$result) {
        echo "Error uploading file";
        exit;
        }
        if(!get_magic_quotes_gpc())
        {
        $fileName = addslashes($fileName);
        $filePath = addslashes($filePath);
        }
    
        $sql_program = "INSERT INTO program (program_name, program_description,filepath) 
        VALUES ('$program_name', '$program_description','$filePath' )";
    
    
        mysql_query($sql_program,$db) or die('Error, query failed ');
        }
        ?>
<!-- Image Form -->

 <form name="Image" enctype="multipart/form-data" action="image_programs.php" method="POST">
                <tr>
                    <td>
                    <input type="File" name="Photo" size="2000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26"></td>
                </tr>
                
                <tr>
                    <td width="100">Program Name</td>
                    <td><input class="commentarea" name="program_name" type="text" id="program_name"></td>
                </tr>
                
                <tr>
                    <td width="100">Program Description</td>
                    <td><input class="commentarea" name="program_description" type="text"      id="program_description"></td>
                </tr>
                
                <br/>
                
                <tr>
                    <td>
                    <INPUT type="Submit" class="button" name="Submit" value="Submit">
                    </td>
                </tr>          
   </form>
10
  • Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); see if it yields anything. Also or die(mysql_error()) to mysql_query(). Commented Oct 29, 2014 at 19:38
  • These are the returned results:Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\SMLC\includes\image_programs.php on line 10 Warning: move_uploaded_file(.../smlc/images/Uploaded/programs/pew.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\SMLC\includes\image_programs.php on line 23 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php4A35.tmp' to '.../smlc/images/Uploaded/programs/pew.jpg' in C:\xampp\htdocs\SMLC\includes\image_programs.php on line 23 Error uploading file Commented Oct 29, 2014 at 19:46
  • mysql_select_db($database,$db); that will fix the 1st error. $uploadDir = 'images/Uploaded/programs/'; and your error is Unable to move 'C:\xampp\tmp\php4A35.tmp' to '.../smlc/images/Uploaded/programs/pew.jpg' not the same folder. Plus, seems to have 3 dots in there. Commented Oct 29, 2014 at 19:47
  • Sidenote:: On Linux, Uploaded is not the same as uploaded, should your folder be all in lowercase. Commented Oct 29, 2014 at 19:49
  • Do I need to have my image/uploaded/programs folder located inside my includes? I would prefer not to do that... Commented Oct 29, 2014 at 19:52

1 Answer 1

1

Try this

$uploadDir=dirname(__FILE__)."/images/Uploaded/programs/";

and images folder should be in same folder that your current file located
Uploaded and uploaded are not same name depending on server.
So take care about your folder names too.

Sign up to request clarification or add additional context in comments.

4 Comments

Still the same error... Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\SMLC\image_programs.php on line 38 Warning: mysql_error() expects parameter 1 to be resource, string given in C:\xampp\htdocs\SMLC\image_programs.php on line 38
I actually already changed the folder name to lowercase for continuity... thank you though
It seems your upload problem fixed.try using this mysql_query($sql_program) instead of mysql_query($sql_program,$db)
mysqli_query($db, $sql_program); in sqli syntax the $db parameter needed to to be listed first. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.