Uploading the pictures in my web site from admin user interface

Discussion in 'PHP' started by Ngajel, Aug 1, 2010.

  1. #1
    I am having problem how to display the pictures in my website from the admin interface. I have designed the admin user interface to post the latest news. As soon as i post, it will save in the database and then retrieve from the table and display in my web page. But i am not able to retrieve my pictures which i have uploaded.

    I have uploaded my pictures not in the table but in same directory called 'upload' folder. Please any one can help me.
     
    Ngajel, Aug 1, 2010 IP
  2. pdjsolutions

    pdjsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, you need to store the filename inside the database, and then retrieve the same when you pull the record from the DB. just target the img source to the file name :) .

    Hope that helps.

    Prateek
    PDJSolutions.
     
    pdjsolutions, Aug 1, 2010 IP
  3. Ngajel

    Ngajel Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Dear PDJSolutions. Thanks a lot for your prompt response. for your kind information, i have written the php code to save the pictures in folder not in database as mentioned below:
    <?php
    //?heck that we have a file
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {

    //Check if the file is JPEG image and it's size is less than 350Kb

    $filename = basename($_FILES['uploaded_file']['name']);
    $ext = substr($filename, strrpos($filename, '.') + 1);
    if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
    ($_FILES["uploaded_file"]["size"] < 350000)) {

    //Determine the path to which we want to save this file
    $newname = dirname(__FILE__).'/upload/'.$filename;

    //Check if the file with the same name is already exists on the server
    if (!file_exists($newname)) {

    //Attempt to move the uploaded file to it's new place
    if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {

    // echo "It's done! The file has been saved as: ".$newname;
    } else {
    echo "Error: A problem occurred during file upload!";
    }
    } else {
    echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
    }
    } else {
    echo "Error: Only .jpg images under 350Kb are accepted for upload";
    }
    } else {
    //echo "Error: No file uploadedfff";
    }
    ?>

    But as you said that need to store the file-name inside the database, i couldn't do that and please help me.
     
    Ngajel, Aug 1, 2010 IP
  4. pdjsolutions

    pdjsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Right,

    Here you got the file name as .$newname right ? Now you need to store that into your database. For example, you are making a post and the same way that gets store into your database, you need to create a field for storing the image file name. And then for it to appear in the web page, the same way you retrieved the post details, you need to retrieve the name and then use that.

    If you need further help. PM me the details to your admin interface and i will help you sort out the problem.

    Prateek
    PDJSolutions
     
    pdjsolutions, Aug 1, 2010 IP
  5. Ngajel

    Ngajel Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Dear Prateek,
    Here is my admin interface details.
    <?php include "includes/header.php";?>
    <?php include "includes/tops.php";?>
    <table width="68%" align="center">
    <tr valign="top">
    <td background="images/Web.jpg"><a href="index.php" style="color:#000000; text-decoration: none; padding-left: 10px;"><strong>Home Page</strong></a></td>
    </tr>
    </table>
    <?php

    include "conf/configuration.php";

    if($_POST['register'])
    {
    $topic = $_POST['Top'];
    $brief = $_POST['brief'];
    $more = $_POST['more'];
    $pic = $_POST['pics'];
    $id = $_POST['id'];

    //if($name==""){ echo "Please Enter your Name";}
    //elseif($contNo==""){ echo "Please Enter your contact Number";}
    //elseif($email==""){ echo "Please Enter your Email Address";}
    //elseif($comments==""){ echo "Please Enter your comments";}

    //else
    //{
    $qry = "Insert into news(newsID,Topic,Brief,Detail) values('$id','$topic','$brief','$pic')";

    echo $qry;
    mysql_query($qry) or die("Cannot insert the records, please try again");

    echo "<script type='text/javascript'>";
    //echo "alert('Your feedback is send successfuly')";
    echo "location.href = ('confirmmessage.php')";
    echo "</script>";
    //}
    }
    ?>

    <?php
    include "upload.php" //called uploading picture code as i have posted to you earlier.
    ?>
    <form enctype="multipart/form-data" action="<?php $PHP_SELF;?>" method="post">
    <table cellpadding="0" cellspacing="0" border="0" width="68%" align="center">
    <tr>
    <td align="center"valign="middle">
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td class="plain_txt">
    Topic:
    </td>
    <td>
    <input type="text" name="Top" maxlength="50">
    </td>
    </tr>
    <tr>
    <td align="left" class="plain_txt">
    In Brief: &nbsp;
    </td>
    <td>
    <input type="text" name="brief" size="43">
    </td>
    </tr>
    <tr>
    <td align="left" class="plain_txt">
    Details:
    </td>
    <td align="left">
    <textarea rows="6" cols="36" name="more" ></textarea>
    </td>
    </tr>
    <tr>
    <td class="plain_txt">Browse: </td>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <td><input name="uploaded_file" type="file" />
    </td>
    </tr>
    <tr>
    <td>
    </td>
    <td align="left" height="31" valign="top">
    <input type="submit" class="btn" name="register" value="Post News" id="submit">
    <input type="reset" value="Cancel" name="cancel" class="btn" id="cancel" onClick="CancelForm()">
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </form>
    <?php
    include "includes/adminbottom.php";
    ?>

    please help to me.
     
    Ngajel, Aug 1, 2010 IP