It seems it only executes the first line in which for example in uploading a .PNG photo it changes the format to Jpeg....I guess it has something to do with my if elseif else statement.....help :p

<?php

            if (isset($_POST['submit'])) {
                $name= $_POST['name'];
                $album_id=$_POST['album'];
                $file=$_FILES['file']['name'];
                $file_type=$_FILES['file']['type'];
                $file_size=$_FILES['file']['size'];
                $file_tmp=$_FILES['file']['tmp_name'];
                $random_name=rand();



                if(!empty($name) or !empty($file) AND $_FILES["file"]["type"] == "image/jpg" ){
                    move_uploaded_file($file_tmp,'uploads/'.$random_name.'.jpg');
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name.jpg')");
                    echo "Photo Uploaded!";
                }
                else if(!empty($name) or !empty($file) AND $_FILES["file"]["type"] == "image/png" ){
                    move_uploaded_file($file_tmp,'uploads/'.$random_name.'.png');
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name.png')");
                    echo "Photo Uploaded!";
                }
                else if(!empty($name) or !empty($file) AND $_FILES["file"]["type"] == "application/docx" ){
                    move_uploaded_file($file_tmp,'uploads/'.$random_name.'.docx');
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name.docx')");
                    echo "Docu Uploaded!";
                }
                else
                    echo "Please fill everything or invalid photo format <br><br>";

            }

        ?>

Use || or && for if statemets (OR/AND are for SQL)

if(!empty($name) || (!empty($file) && $_FILES["file"]["type"] == "application/docx")){

Welcome :D

oh wait..it still doesnt work it doesnt read my else if statement...I did it a little different this time though for easy reading..help!hehe...

<?php

            if (isset($_POST['submit'])) {
                $name= $_POST['name'];
                $album_id=$_POST['album'];
                $file=$_FILES['file']['name'];
                $file_type=$_FILES['file']['type'];
                $file_size=$_FILES['file']['size'];
                $file_tmp=$_FILES['file']['tmp_name'];
                $random_name=rand();

                if(!empty($name)){
                    if (!empty($file)) {
                        if ($_FILES["file"]["type"]=="image/jpg") {
                            move_uploaded_file($file_tmp,'uploads/'.$random_name.'.jpg');
                            mysql_query("INSERT into photos values('','$name','$album_id','$random_name.jpg')");
                            echo "Photo Uploaded!";
                        }
                        elseif ($_FILES["file"]["type"]=="image/png") {
                            move_uploaded_file($file_tmp,'uploads/'.$random_name.'.png');
                            mysql_query("INSERT into photos values('','$name','$album_id','$random_name.png')");
                            echo "Photo Uploaded!";
                        }
                        elseif ($_FILES["file"]["type"]=="application/docx") {
                            move_uploaded_file($file_tmp,'uploads/'.$random_name.'.docx');
                            mysql_query("INSERT into photos values('','$name','$album_id','$random_name.docx')");
                            echo "Word file Uploaded!";
                        }
                    }else
                            echo "WARNING: Only PNG and JPEG format are allowed";
                }
        }
?>

One too many } before the last else. If you line up your indented { with } you can easily spot these mistakes.

So I finally got it to work lol...the problem was the jpg...it should be jpeg....Sensei please check my improved code and suggest a way to make it better..im going to use includes for the phps...

<?php
error_reporting(0);
mysql_connect('localhost','root','');
mysql_select_db('uslt');
?>
<html>
<head>

    <title>Album Gallery</title>
<link rel='stylesheet'  href='style.css'>
</head>
<body>
<div id='body'>
    <?php include('title_bar.php');?>
    <div id='container'>                                                                                                                              
        <h3>Upload Photos</h3>
        <form enctype='multipart/form-data' method='post'>
        <?php

            if (isset($_POST['submit'])) 
            {
                $name= $_POST['name'];
                $album_id=$_POST['album'];
                $file=$_FILES['file']['name'];
                $file_type=$_FILES['file']['type'];
                $file_size=$_FILES['file']['size'];
                $file_tmp=$_FILES['file']['tmp_name'];
                $random_name=rand();

                if(!empty($name))
                {
                    if (!empty($file)) 
                    {
                        if ($_FILES["file"]["type"]=="image/jpeg") 
                        {
                                   move_uploaded_file($file_tmp,'uploads/'.$random_name.'.jpeg');
                            mysql_query("INSERT into photos values('','$name','$album_id','$random_name.jpeg')");
                            echo "Photo Uploaded!";
                        }
                        elseif ($_FILES["file"]["type"]=="image/png") 
                        {
                            move_uploaded_file($file_tmp,'uploads/'.$random_name.'.png');
                            mysql_query("INSERT into photos values('','$name','$album_id','$random_name.png')");
                            echo "Photo Uploaded!";
                        }
                        else
                            echo "WARNING: Only PNG and JPEG format are allowed<br><br>";         
                    }       
                }
                else
                    echo "WARNING: You either didn't choose a file or name the photo <br><br>";
            }
        ?>
            Name: <br>
            <input type='text' name='name'>
            <br><br>
            Select Album:<br>
            <select name='album'>
                <?php
                    $query=mysql_query("SELECT id, name from albums");
                    while ($run=mysql_fetch_array($query)) {
                        $album_id=$run['id'];
                        $album_name=$run['name'];
                        echo "<option value='$album_id'>$album_name</option>";
                    }
                ?>
            </select>
            <br><br>
            Select Photo: <br/>
            <input type='file' name='file'>
            <br><br>
            <input type='submit' name='submit' value='Upload'>
        </form>
    </div>

</div>
</body>
</html>
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.