Hello Daniweb,,

I have uploaded an image using the student_id as its filename. The student id is auto-incremented and unique per student_id. Now i am confused why it gives me problem to display it. I have tried.

  <?php print '<img src="uploads/'.$session_id.'" />' ?>

  <?php print '<img src="uploads/'.$session_id.".".$ext'" />' ?>

  <?php print '<img src="uploads/'.$student_id.'" />' ?>

  <img src='uploads/".$actual_image_name."' class='profile-photo' align="middle"> //where $actual_image_name = $student_id.".".$ext;

but still the image wont display

<?php print '<img src="uploads/'.$session_id.".".$ext'" />' ?>

A file extension would be a good idea, so assuming (as @pirateas has said) $session_id = $student_id, and you have an image saved in uploads/{student_id}.{extension}, all of that looks fine.

Member Avatar for diafol

Are you sure that you're referencing the uploads directory properly? Is uploads a directory in the same directory as the page you're on? Try modifying to display a hardcoded image...

<?php print '<img src="uploads/836.jpg" />' ?>

i.e. one that you know exists in the directory. Sorry perhaps that's too simplistic. There can be issues, especially if you're using apache mod_rewriting. If so you may need to have a list of exempt directories from your rewriting.

@pritaeas
@mattster

i think to answer better your queries is to give you my upload.php. This is a succesful page that when the user click the upload button the image will automatically display on same page temporarily and renamed to the unique student_id.

<?php
    session_start();
    $session_id = $_SESSION['user_id'];
    if($session_id == null){
       header("location:Student_Edit.php");
       die();
    }
    include 'Connect.php';
    $flag = "";
    $result = mysql_query("SELECT * FROM student_information where student_id='{$_SESSION['user_id']}'",$link_id);;
    $data = mysql_fetch_array($result);
?>   

---some html, javascript and style codes here

<?php
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024)) // Image size max 1 MB
{
$session_id = $student_id.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
mysql_query("UPDATE student_information SET profile_image='$actual_image_name' WHERE student_id='{$_SESSION['user_id']}'",$link_id);
echo "<img src='uploads/".$session_id."' class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB"; 
}
else
echo "Invalid file format.."; 
}
else
echo "Please select image..!";
exit;
}
?>
<div style="width:600px">
<form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
Change your profile pic <input type="file" name="photoimg" id="photoimg" />
</form>
<div id='preview'>
</div>  

</body>
</html>

Now my problem is to display the codes on the other page.

@diafol,

after i uploaded the image it displays automatically on the same page and the filename will be rename to its student_id (the one who uploads or log in)

i have followed your suggestion by directly referring the filename of the uploaded image and whew!!! it works and the image displays.

i am very interested in your statement to modify my codes in regards to this kind of apache situation. please be kind to modify my codes.

Member Avatar for diafol

Sorry I don't do that as a rule. If you're not using mod_rewrite, then there's no point.

@diafol:

sorry if i ask you for that, if in any case, is there any way that someone could give me the codes, a donation or so?
is it still possible to display the uploaded image to other page?

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.