Dear,

I make a php page to upload the images, pdf, swf etc etc for specific orginisation entered in same form.

In the form i made the input type files to upload a maximum of three files but i know how to upload one of them only. here i upload my code used for upload form page called nework.php and the process page called addworkprocess.php

anyone help me how to upload the second and third file in the same time with addworkprocess.php? Apart from that part from process page i made the types of images like png and jpeg, anyone tell me how to make it to upload swf and pdf files? I appriciate your help.

here is the code of form page:

<?php
    require("headerloggedin.php");
?>
<?php

?>
            <div id="content">
                <div id="topcontent">
                    <p id="titles" align="center">Add Category</p>
                        <form enctype="multipart/form-data" method="post" action="addworkprocess.php">
                            <input type="hidden" name="idofuser" value="<?php echo $userid;?>"/>
                            <table align="center" bgcolor="#999999">
                                <tr>
                                    <td id="normaltxt">Organisation Name:</td> 
                                    <td><input type="text" name="organisation"  /></td>
                                </tr>
                                <tr>
                                    <td id="normaltxt">Description of Work:</td> 
                                    <td><input type="text" name="description"  /></td>
                                </tr>
                                <tr>
                                    <td id="normaltxt">Add First Work:</td>
                                    <td width="50px"><input type="file" name="first" id="file"></td>
                                </tr>
                                <tr>
                                    <td id="normaltxt">Add Second Work:</td>
                                    <td width="50px"><input type="file" name="second" id="file"></td>
                                </tr>
                                <tr>
                                    <td id="normaltxt">Add Third Work:</td>
                                    <td width="50px"><input type="file" name="third" id="file"></td>
                                </tr>
                                <tr>
                                    <td colspan="2"><input id="editfrmbtn" type="submit" value="Add" /></td>
                                </tr>
                            </table>
                        </form>
                </div>
            </div>
<?php
    require("footerloggedin.php");
?>

and here is the process page:

<?php 
    require("headerloggedin.php"); 
?>
<div id="content">
    <div id="topcontent">
<?php
    $userid = $_POST["idofuser"];
    $nameoforganisation = $_POST["organisation"];
    $description = $_POST["description"];
    $allowedExts = array("gif", "jpeg", "jpg", "png", "pdf", "swf");

    $foldername = "images/icons/";
    $imgname = ($_FILES["first"]["name"]);
    $imgtype = ($_FILES["first"]["type"]);
    $imgsize = ($_FILES["first"]["size"]);
    $extension = end(explode(".", $imgname));

    if ((($imgtype == "image/gif")
            || ($imgtype == "image/jpeg")
            || ($imgtype == "image/jpg")
            || ($imgtype == "image/pjpeg")
            || ($imgtype == "image/x-png")
            || ($imgtype == "image/png")
            || ($imgtype == "application/pdf")
            || ($imgtype == "application/swf"))
            && ($imgsize < 200000000)
            && in_array($extension, $allowedExts))
    {
        if ($_FILES["first"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["first"]["error"] . "<br>";
        }
        else
        {
            echo "Upload: " . $imgname . "<br>";
            echo "Type: " . $imgtype . "<br>";
            echo "Size: " . ($imgsize / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["first"]["tmp_name"] . "<br>";

            if (file_exists($foldername . $imgname))
            {
                echo $imgname . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["first"]["tmp_name"],"images/icons/" . $imgname);

                $query = "INSERT INTO gallery (userid, orgname, description, folder, file, name) VALUES ('$userid', '$nameoforganisation', '$description', '$foldername', '$imgname', '$imgtype')";

                $db->query($query);
                echo "Stored in: " . $foldername . $imgname;
            }
        }
    }
    else
    {
        echo "Invalid file";
    }
?>
    </div>
</div>
<?php 
    require("footerloggedin.php"); 
?>

Thanks for your help.

marifard

Recommended Answers

All 12 Replies

I am posting how i handled it, you can change as your need

First page code, I use number instead of text in file name

<input type=file name="0" class="input"  >   <br>
<input type=file name="1" class="input"  >   <br>
<input type=file name="2" class="input"  >   <br>
<input type=file name="3" class="input"  >   <br>
<input type=file name="4" class="input"  >   <br>

So in process page i can get array of file inputs

$filecount=count($_FILES);

    for($j=0;$j<$filecount;$j++)
    {   
        if($_FILES[$j]['size']<=0 ) 
            continue;
        $fileno=$j+1;
        $filetitle = $_FILES[$j]['name'];


        if($filetitle!="")
        {       
            $fileext = substr($_FILES[$j]['name'], strrpos($_FILES[$j]['name'], '.'));
            $uploaddir = "../pdf/mail_attach/";
            $max_size = "4194304";//"2621440";//"8388608";//""6291456";//"5242880";//"1048576";//"512000";//8388608

            if ($_FILES[$j]['type'] == "text/html")
            {
                $var_msg.= "Incorrect File Extension for file {$fileno}!<br>";
                $insert=false;
                break;
            }
            else
            {
                if($_FILES[$j]['size'] > $max_size)
                {
                    $var_msg.= "File Size Is Too Big for file {$fileno}! Max size is 4 MB<br>!";
                    $insert=false;
                    break;

                }
                else
                {



                    move_uploaded_file($_FILES[$j]['tmp_name'],$uploaddir.$file_name);                          





                }
            }   
        }   
    }//end file array

how to restore the imges etc etc in db?

You place your single file code in the for loop i set, check all variables carefully and you are done

Dear urtrivedi,

I assume that code is used for pdf files only? if yes how to upload files like images too such as jpg, png etc etc?

thanks.

ok i made some improvments but i have some difficults. in this code shown later in this post, it uploads 1 file only in db. the columns of the table are: orgid, userid, orgname, description, folder, file, extension, dateuploaded.

to upload three file for same orginisation i multiply folder, file, extension columns by 2? I mean, i make table like this? orgid, userid, orgname, description, folder, file, extention, folder1, file1, extension1, folder2, file2, extension 2, dateuploaded ?

this is the code:

<?php 
    require("headerloggedin.php"); 
?>
<div id="content">
    <div id="topcontent">
<?php
        $nameoforganisation = $_POST["organisation"];
        $description = $_POST["description"];
        $filecount=count($_FILES);
        for($j=0;$j<$filecount;$j++)
        {   
            if($_FILES[$j]['size']<=0 ) 
            continue;
            $fileno=$j+1;
            $filetitle = $_FILES[$j]['name'];
            if($filetitle!="")
            {       
                $fileext = substr($_FILES[$j]['name'], strrpos($_FILES[$j]['name'], '.'));
                $uploaddir = "images/icons/";
                $max_size = "4194304";                  //"2621440";//"8388608";//""6291456";//"5242880";//"1048576";//"512000";//8388608
                echo $fileext;
                if ($_FILES[$j]['type'] == "text/html")
                {
                    $var_msg.= "Incorrect File Extension for file {$fileno}!<br>";
                    $insert=false;
                    break;
                }
                else
                {
                    if($_FILES[$j]['size'] > $max_size)
                    {
                        $var_msg.= "File Size Is Too Big for file {$fileno}! Max size is 4 MB<br>!";
                        $insert=false;
                        break;
                    }
                    else
                    {
                        /*move_uploaded_file($_FILES[$j]['tmp_name'],$uploaddir.$file_name);   */   
                        move_uploaded_file($_FILES[$j]['tmp_name'], $uploaddir.$filetitle.$fileext);

                        $query = "INSERT INTO gallery (userid, orgname, description, folder, file, extension) VALUES ('$userid', '$nameoforganisation', '$description', '$uploaddir', '$filetitle', $fileext)";

                        $db->query($query);
                        echo "Stored in image file ";                   
                    }
                }   
            }   
        }//end file array
?>
    </div>
</div>
<?php 
    require("footerloggedin.php");
?>

My table structure was common, I dont add 5x5 columns in table I wil keep only 5 colums, and I will add 5 records for that

This is perfect table design

Hi,

I made it and works perfect but I try to add something with this code. I made a column in my gallery table called dateuploaded and type of this column is timestamp/CURRENT_TIMESTAMP to save the date and time when the image is uploaded in database but when I try to upload an image, the page show me this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10:51:04)' at line 1

I made this code:

<?php 
    require("headerloggedin.php"); 
?>
<div id="content">
    <div id="topcontent">
    <?php
        $clientid = $_POST["idofclient"];
        $month = $_POST["month"]; 
        $year = $_POST["year"];

        //$month and $year is used to post the month and year when the work is finished.

        $currdatetime = date("Y/m/d H:i:s"); //the date and time when the image is uploaded in db

        $filecount=count($_FILES);
        for($j=0;$j<$filecount;$j++)
        {   
            if($_FILES[$j]['size']<=0 ) 
            continue;
            $fileno=$j+1;
            $filetitle = $_FILES[$j]['name'];
            if($filetitle!="")
            {       
                $fileext = substr($_FILES[$j]['name'], strrpos($_FILES[$j]['name'], '.'));
                $uploaddir = "../images/work/";
                $max_size = "108388608";//"2621440";//"8388608";//""6291456";//"5242880";//"1048576";//"512000";//8388608

                if ($_FILES[$j]['type'] == "text/html")
                {
                    $var_msg.= "Incorrect File Extension for file {$fileno}!<br>";
                    $insert=false;
                    break;
                }
                else
                {
                    if($_FILES[$j]['size'] > $max_size)
                    {
                        $var_msg.= "File Size Is Too Big for file {$fileno}! Max size is 4 MB<br>!";
                        $insert=false;
                        break;
                    }
                    else
                    {
                        /*move_uploaded_file($_FILES[$j]['tmp_name'],$uploaddir.$file_name);   */   
                        move_uploaded_file($_FILES[$j]['tmp_name'], $uploaddir.$filetitle);

                        $query = "INSERT INTO gallery (clientid, folder, file, filext, month, year, dateuploaded) VALUES ('$clientid', 'images/work/', '$filetitle', '$fileext', '$month', '$year', $currdatetime)";

                        $db->query($query);
                        echo "The file with the name of ".$filetitle." is uploaded.";             
                    }
                }   
            }   
        }//end file array
?>
    </div>
    <div id="btmcontent">
        <a id="backbtn" href="galleryloggedin.php">Back</a>
    </div>
</div>
<?php 
    require("footerloggedin.php");
?>

change query to following

$query = "INSERT INTO gallery (clientid, folder, file, filext, month, year, dateuploaded) VALUES ('$clientid', 'images/work/', '$filetitle', '$fileext', '$month', '$year', current_timestamp())";

I made it but the time is incorrect because the time uploaded is 21:24 but on db saves 15:24. How to make my country time on table? Apart from that, it is possible to save the date in table like DD-MM-YYYY instead of YYYY-MM-DD?

Thanks

mysql default format is yyyy-mm-dd and when you want to display somewhere, you can use date_format(datecolumn,'%d-%m-%Y') as mydate

for country time, you need to set timezone of your contry in the server

Hi,
I make the correction when inserting so time in the database records is always local time:

$TZADD = 6; // difference in hours from client to database server

// add this in insert query
"created = TIMESTAMPADD(HOUR,".$TZADD.",NOW())"

This works fine if $TZADD is constant. However GoDaddy hosting has its database servers in one of the few locations in the US that does not use Daylight Savings Time so $TZADD needs to change twice a year. I have a solution if anyone needs.

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.