0

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>";

            }

        ?>
3
Contributors
6
Replies
36
Views
3 Years
Discussion Span
Last Post by RonKevinT.Manuela
1

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

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

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";
                }
        }
?>

Edited by RonKevinT.Manuela: cut

1

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

1

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>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.