Okay so a few days ago I finished a tutorial of photo gallery...I'm having problems of putting images other than jpg format...the one who made the tutorial said that you can add formats to be uploaded but he didnt show it..here was his code in his upload.php...I tried experimenting on it but I cant get it to work properly..

<?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) or empty($file)) {
                    echo "Please fill everything <br><br>";

                }else{
                    move_uploaded_file($file_tmp,'uploads/'.$random_name. '.jpg');
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name.jpg')");
                    echo "Photo Uploaded!";
                }
            }

        ?>
            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>

I don't see a restriction in the code. I think you just have to replace the hardcoded .jpg with $_FILES['file']['type']

uhmm..what? i kinda tried it but maybe i did the wrong way..what exactly do you mean pritaes-sensei?

i kinda tried it but maybe i did the wrong way.

Show what you've tried.

move_uploaded_file($file_tmp,'uploads/'.$random_name. '.$_FILES['file']['type']');
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name.$_FILES['file']['type']')");

oh wait nevermind...I think I know what to do...wait...

false alarm lol...I still dont know how

oh wait i got it to work..hmmm...i removed .jpeg..i am able to upload other photos..i also tried word file though it isnt necessary for this photo gallery it gioves me a rar file lol...i wonder why

move_uploaded_file($file_tmp,'uploads/'.$random_name);
                    mysql_query("INSERT into photos values('','$name','$album_id','$random_name')");
  1. rand() will return a random number so your query will be: 'uploads/12324'). You need to put a file extension after it.
  2. Also, don't use this, because you might get two image with the same random number, and they'll overwrite each other.

You can't have $variable.'.$variable etc., because it will just think your second variable is text - try this instead:

move_uploaded_file($file_tmp,'uploads/'.$random_name.$_FILES['file']['type']);

PHP will stick the inverted commas in itself, so just stick thr variables in without 's either side of them.

thanks should I do the same with mysqlquery?

Yes you should.

Then just something like: <img src="<?= $photo['url']" ?>

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.